网站推广.NET

网站推广.NET

layui常用方法

来源:互联网

layui(谐音:类ui) 是一款采用自身模块规范编写的前端 ui 框架,遵循原生 html/css/js 的书写与组织形式,门槛极低,拿来即用。

layui常用的方法:

layui中的input radio单选框监听选择触发事件:

根据给input绑定的 lay-filter 进行查找input,然后进入函数判断

<p class="layui-form-item">            <label class="layui-form-label">长期短期</label>            <p class="layui-input-block">                <input type="radio" name="term" value="长期" title="长期有效"                    lay-filter="term"> <input type="radio" name="term"                    value="短期" title="短期有效" lay-filter="term">            </p>        </p>        <p id="termtime" class="layui-hide">            <p id="classdate" class="layui-form-item" >                <label class="layui-form-label">开始时间:</label>                <p class="layui-input-block">                    <input type="text" name="startdate" id="startdate"                        lay-verify="startdate" autocomplete="off" class="layui-input">                </p>                <label class="layui-form-label">结束时间:</label>                <p class="layui-input-block">                    <input type="text" name="enddate" id="enddate"                        lay-verify="enddate" autocomplete="off" class="layui-input">                </p>            </p>        </p>
form.on(&#39;radio(term)&#39;, function(data) {            if (data.value == "短期") {                $("#termtime").removeClass("layui-hide");            } else if (data.value == "长期") {                $("#termtime").addClass("layui-hide");            }        });

设置按钮根据状态条件显示不同的按钮:

<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>

各种弹窗:

注:因为有的模块中已经声明layer有的则没有,所以这里都用layui.layer的方式来调用layer弹出层:

半透明黑色背景提示框,666毫秒自动关闭:

layui.layer.msg(returndata.msg,{time: 666});

蓝色模块,左边跳出动画,有确定按钮

layui.use(&#39;layer&#39;, function() {                 layer.alert(returndata.msg, {                        skin: &#39;layui-layer-lan&#39;                        ,closeBtn: 0                        ,anim: 4 //动画类型                      });                });

input只能输入数字,不能输入其他和小数点

<input type="text" id="num" name="num" placeholder="请输入" autocomplete="off" class="layui-input"  onkeyup="this.value=this.value.replace(/\D/g,&#39;&#39;)" onafterpaste="this.value=this.value.replace(/\D/g,&#39;&#39;)">

layui遮罩层,上传开始,成功后关闭遮罩层

<script type="text/javascript">function  tanchu(){    layui.use(&#39;layer&#39;, function(){          var layer = layui.layer;                    var index = layer.load(1, {              shade: [0.1,&#39;#fff&#39;] //0.1透明度的白色背景            });        }); }function  tanchuclose(){    layui.use(&#39;layer&#39;, 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: &#39;<%=basePath%>/knowledge/upload&#39; ,                    type: &#39;POST&#39;,                    data: formData,                    async: false,                    cache: false,                    contentType: false,                    processData: false,                    success: function (returndata) {                                                      tanchuclose();                      alert(returndata);                  },                    error: function (returndata) {                        alert(returndata);                    }               });          }                    </script>

点击修改:

if(obj.event === &#39;setSign&#39;){        layer.prompt({            formType: 2            ,title: &#39;修改 ID 为 [&#39;+ data.id +&#39;] 的角色页面&#39;            ,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(&#39;idTest&#39;, {                        //url: &#39;../user/selectmsguser.do&#39;                        url: &#39;<%=basePath%>/sys/role/list1?q=1&#39;                        ,where: {}                         //,height: 300                      });                },                error:function(msg) {                    cosole.log(msg);                }            });             obj.update({              sign: value            });          });        }

格式化日期到时分秒:

<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+&#39;/&#39;+m+&#39;/&#39;+d1+&#39;  &#39;+HH+&#39;:&#39;+mm+&#39;:&#39;+ss;}; if(true){ }} {{ fn() }}{{#  } }}</script>

格式化时间日期:

<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+&#39;/&#39;+m+&#39;/&#39;+d1;}; if(true){ }} {{ fn() }}{{#  } }}</script>

密码显示 ***** :

<script type="text/html" id="pwd"> {{#  var fn = function(){  return &#39;***&#39;;}; if(true){ }} {{ fn() }}{{#  } }}   </script>

状态栏:

<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>

远程获取数据:

<script type="text/javascript">        layui.use(&#39;form&#39;, 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=&#39;tname&#39; value="+data.data[a].id+" >"+ data.data[a].urealname + "</option>")                    }                    form.render(&#39;select&#39;); //用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=&#39;name&#39; value="+data.data[a].id+" >"+ data.data[a].urealname + "</option>")                    }                    form.render(&#39;select&#39;); //用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=&#39;tname&#39; value="+data.data[a].cid+" >"+ data.data[a].cname + "</option>")                    }                    form.render(&#39;select&#39;); //用ajax追加的需要这样渲染一下                                    },                error : function() {                }            })                        //form.on(&#39;select(username)&#39;, function(data) {//给隐藏的input赋值(机构id)                //$("#yincang").val(data.value);            //});                    });    </script>

表单获取Value数据:

function formLoad(element,data){   var input = document.getElementById(element).getElementsByTagName(&#39;input&#39;);   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];         }          }       }  }

这是搜索按钮,点击触发重载:

<p class="demoTable">                        搜索角色:                        <p class="layui-inline">                            <input class="layui-input" name="name" id="demoReload"                                autocomplete="off">                        </p>                        <button class="layui-btn" style="transform: translateY(-10px);"                            data-type="reload">搜索</button>                    </p>

这是重载事件:

不需要指定重载的url,只需要根据表格的ID就可以重载,这里的ID是在layui声明的的ID,不是普通的标签中这样的id,然后再where里面传参数

<script>layui.use(&#39;table&#39;, function(){  var table = layui.table;  var $ = layui.$, active = {    reload: function(){      var demoReload = $(&#39;#demoReload&#39;);            //执行重载      table.reload(&#39;idTest&#39;, {        page: {          curr: 1 //重新从第 1 页开始        },        where: {          name:  demoReload.val()         }      });    }  };    $(&#39;.demoTable .layui-btn&#39;).on(&#39;click&#39;, function(){    var type = $(this).data(&#39;type&#39;);    active[type] ? active[type].call(this) : &#39;&#39;;  });});</script>

添加或修改打开一个页面后,关闭页面重载原来页面数据表格

$("#addBookbtn").click(function(){          layer.open({              type: 2,              title:[&#39;添加新书信息&#39;,&#39;font-size:22px&#39;],              area: [&#39;400px&#39;, &#39;420px&#39;],              content: &#39;../jsp/addbook.jsp&#39;,              cancel: function(index, layero){                   layer.confirm(&#39;是否关闭?&#39;, {icon: 3, title:&#39;提示&#39;}, function(index){                      layer.close(index);                      table.reload("booktable");                    });                }               });      });

更多layui相关知识请关注layui框架

layui教程