Common methods of layui
layui (homophone: UI-like) is a front-end UI framework written using its own module specifications. It follows the writing and organizational form of native HTML/CSS/JS. The threshold is extremely low and can be used out of the box.
Commonly used methods in layui:
The input radio radio button in layui listens for the selection trigger event:
Search the input based on the lay-filter bound to the input, and then enter the function judgment
<div class="layui-form-item"> <label class="layui-form-label">长期短期</label> <div class="layui-input-block"> <input type="radio" name="term" value="长期" title="长期有效" lay-filter="term"> <input type="radio" name="term" value="短期" title="短期有效" lay-filter="term"> </div> </div> <div id="termtime" class="layui-hide"> <div id="classdate" class="layui-form-item" style=""> <label class="layui-form-label">开始时间:</label> <div class="layui-input-block"> <input type="text" name="startdate" id="startdate" lay-verify="startdate" autocomplete="off" class="layui-input"> </div> <label class="layui-form-label">结束时间:</label> <div class="layui-input-block"> <input type="text" name="enddate" id="enddate" lay-verify="enddate" autocomplete="off" class="layui-input"> </div> </div> </div>
form.on('radio(term)', function(data) { if (data.value == "短期") { $("#termtime").removeClass("layui-hide"); } else if (data.value == "长期") { $("#termtime").addClass("layui-hide"); } });
Set the button to display different buttons according to the status conditions:
<script type="text/html" id="toolbtn"> {{# if(d.state == true){}} <a class="layui-btn layui-btn-mini layui-btn-danger" lay-event="down">下线</a> {{# } else { }} <a class="layui-btn layui-btn-mini " lay-event="up">发布</a> {{# } }} </script>
Various pop-up windows:
Note: Because some modules have already declared layer and some have not, the layui.layer method is used here to call the layer pop-up layer:
Translucent black background prompt box, automatically closed in 666 milliseconds:
layui.layer.msg(returndata.msg,{time: 666});
Blue module, animation pops up on the left, with OK button
layui.use('layer', function() { layer.alert(returndata.msg, { skin: 'layui-layer-lan' ,closeBtn: 0 ,anim: 4 //动画类型 }); });
Input can only enter numbers, no other input and decimal point
<input type="text" id="num" name="num" placeholder="请输入" autocomplete="off" class="layui-input" onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')">
layui mask layer, upload starts, close the mask layer after success
<script type="text/javascript"> function tanchu(){ layui.use('layer', function(){ var layer = layui.layer; var index = layer.load(1, { shade: [0.1,'#fff'] //0.1透明度的白色背景 }); }); } function tanchuclose(){ layui.use('layer', function(){ var layer = layui.layer; layer.close(layer.index); }); } </script>
<script type="text/javascript"> function upload(){ tanchu(); var formData = new FormData($( "#uploadForm" )[0]); $.ajax({ url: '<%=basePath%>/knowledge/upload' , type: 'POST', data: formData, async: false, cache: false, contentType: false, processData: false, success: function (returndata) { tanchuclose(); alert(returndata); }, error: function (returndata) { alert(returndata); } }); } </script>
Click to modify:
if(obj.event === 'setSign'){ layer.prompt({ formType: 2 ,title: '修改 ID 为 ['+ data.id +'] 的角色页面' ,value: data.homePage }, function(value, index){ layer.close(index); $.ajax({ type:"post", url:"<%=basePath%>/sys/role/update", data:{role:JSON.stringify(data)}, dataType:"text",//返回的 success:function(data1) { layer.alert(data1.result); table.reload('idTest', { //url: '../user/selectmsguser.do' url: '<%=basePath%>/sys/role/list1?q=1' ,where: {} //,height: 300 }); }, error:function(msg) { cosole.log(msg); } }); obj.update({ sign: value }); }); }
Format date to hour, minute and second:
<script type="text/html" id="timeTpl"> {{# var fn = function(){ var date=new Date(d.uptime); var d1=date.getDate(); var y=date.getFullYear(); var m=date.getMonth() + 1; var HH=date.getHours(); var mm=date.getMinutes(); var ss=date.getSeconds(); return y+'/'+m+'/'+d1+' '+HH+':'+mm+':'+ss; }; if(true){ }} {{ fn() }} {{# } }} </script>
Format time and date:
<script type="text/html" id="timeTpl"> {{# var fn = function(){ var date=new Date(d.createtime); var d1=date.getDate(); var y=date.getFullYear(); var m=date.getMonth() + 1; return y+'/'+m+'/'+d1; }; if(true){ }} {{ fn() }} {{# } }} </script>
Password display** ***:
<script type="text/html" id="pwd"> {{# var fn = function(){ return '***'; }; if(true){ }} {{ fn() }} {{# } }} </script>
Status bar:
<script type="text/html" id="barDemo1"> <a class="layui-btn layui-btn-mini" id="edit" lay-event="edit">保存</a> <a class="layui-btn layui-btn-danger layui-btn-mini" lay-event="del">冻结</a> </script>
<script type="text/html" id="usernameTpl"> <a href="/?table-demo-id={{d.id}}" class="layui-table-link" target="_blank">{{ d.username }}</a> </script>
Remote data acquisition:
<script type="text/javascript"> layui.use('form', function() { var form = layui.form; $("#tname").children().remove(); $.ajax({ type : "post", url : "../user/selectallusersrole.do?role=3", dataType : "json", sync : "false", success : function(data) { for (var a = 0; a < data.data.length; a++) { $("#tname").append( "<option class='tname' value="+data.data[a].id+" >"+ data.data[a].urealname + "</option>") } form.render('select'); //用ajax追加的需要这样渲染一下 }, error : function() { } }) $("#urealname").children().remove(); $.ajax({ type : "post", url : "../user/selectallusersrole.do?role=4", dataType : "json", sync : "false", success : function(data) { for (var a = 0; a < data.data.length; a++) { $("#urealname").append( "<option class='name' value="+data.data[a].id+" >"+ data.data[a].urealname + "</option>") } form.render('select'); //用ajax追加的需要这样渲染一下 }, error : function() { } }) $("#cname").children().remove(); $.ajax({ type : "post", url : "../class/selectclass.do", dataType : "json", sync : "false", success : function(data) { for (var a = 0; a < data.data.length; a++) { $("#cname").append( "<option class='tname' value="+data.data[a].cid+" >"+ data.data[a].cname + "</option>") } form.render('select'); //用ajax追加的需要这样渲染一下 }, error : function() { } }) //form.on('select(username)', function(data) {//给隐藏的input赋值(机构id) //$("#yincang").val(data.value); //}); }); </script>
The form obtains Value data:
function formLoad(element,data){ var input = document.getElementById(element).getElementsByTagName('input'); for(var i =0;i < input.length;i++){ var inputname = input[i].name; for(var j in data){ if(inputname == j){ input[i].value = data[j]; } } } }
This is the search button, click to trigger reload:
<div class="demoTable"> 搜索角色: <div class="layui-inline"> <input class="layui-input" name="name" id="demoReload" autocomplete="off"> </div> <button class="layui-btn" style="transform: translateY(-10px);" data-type="reload">搜索</button> </div>
This is the reload event:
You don’t need to specify the overloaded url, you only need to reload it according to the ID of the table. The ID here is the ID declared in layui, not the id="idno" in the ordinary tag. Then pass the parameters in where
<script> layui.use('table', function(){ var table = layui.table; var $ = layui.$, active = { reload: function(){ var demoReload = $('#demoReload'); //执行重载 table.reload('idTest', { page: { curr: 1 //重新从第 1 页开始 }, where: { name: demoReload.val() } }); } }; $('.demoTable .layui-btn').on('click', function(){ var type = $(this).data('type'); active[type] ? active[type].call(this) : ''; }); }); </script>
Add or modify After opening a page, close the page and reload the original page data table
$("#addBookbtn").click(function(){ layer.open({ type: 2, title:['添加新书信息','font-size:22px'], area: ['400px', '420px'], content: '../jsp/addbook.jsp', cancel: function(index, layero){ layer.confirm('是否关闭?', {icon: 3, title:'提示'}, function(index){ layer.close(index); table.reload("booktable"); }); } }); });
For more layui related knowledge, please pay attentionlayuiframework.
The above is the detailed content of Common methods of layui. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Layui login page jump setting steps: Add jump code: Add judgment in the login form submit button click event, and jump to the specified page through window.location.href after successful login. Modify the form configuration: add a hidden input field to the form element of lay-filter="login", with the name "redirect" and the value being the target page address.

layui provides a variety of methods for obtaining form data, including directly obtaining all field data of the form, obtaining the value of a single form element, using the formAPI.getVal() method to obtain the specified field value, serializing the form data and using it as an AJAX request parameter, and listening Form submission event gets data.

The method of using layui to transmit data is as follows: Use Ajax: Create the request object, set the request parameters (URL, method, data), and process the response. Use built-in methods: Simplify data transfer using built-in methods such as $.post, $.get, $.postJSON, or $.getJSON.

Adaptive layout can be achieved by using the responsive layout function of the layui framework. The steps include: referencing the layui framework. Define an adaptive layout container and set the layui-container class. Use responsive breakpoints (xs/sm/md/lg) to hide elements under specific breakpoints. Specify element width using the grid system (layui-col-). Create spacing via offset (layui-offset-). Use responsive utilities (layui-invisible/show/block/inline) to control the visibility of elements and how they appear.

The difference between layui and Vue is mainly reflected in functions and concerns. Layui focuses on rapid development of UI elements and provides prefabricated components to simplify page construction; Vue is a full-stack framework that focuses on data binding, component development and state management, and is more suitable for building complex applications. Layui is easy to learn and suitable for quickly building pages; Vue has a steep learning curve but helps build scalable and easy-to-maintain applications. Depending on the project needs and developer skill level, the appropriate framework can be selected.

To run layui, perform the following steps: 1. Import layui script; 2. Initialize layui; 3. Use layui components; 4. Import layui styles (optional); 5. Ensure script compatibility and pay attention to other considerations. With these steps, you can build web applications using the power of layui.

The layui framework is a JavaScript-based front-end framework that provides a set of easy-to-use UI components and tools to help developers quickly build responsive web applications. Its features include: modular, lightweight, responsive, and has complete documentation and community support. layui is widely used in the development of management backend systems, e-commerce websites, and mobile applications. The advantages are quick start-up, improved efficiency, and easy maintenance. The disadvantages are poor customization and slow technology updates.

layui is a front-end UI framework that provides a wealth of UI components, tools and functions to help developers quickly build modern, responsive and interactive web applications. Its features include: flexible and lightweight, modular design, rich components, Powerful tools and easy customization. It is widely used in the development of various web applications, including management systems, e-commerce platforms, content management systems, social networks and mobile applications.
