站长图库向大家介绍了如何使用layui,table模块,基础参数等相关知识,希望对您有所帮助

导语:
layui是由职业前端倾情打造,面向全层次的前后端开发者,低门槛开箱即用的前端 UI 解决方案。
layui 的 table模块是一个重头,在基础参数方面尽可能地做到友好,即:保证功能的前提而又避免过于繁杂的配置。基础参数一般出现在以下几种场景中:
场景一:下述 lay-data 里面的内容即为基础参数项,切记:值要用单引号
<table lay-data="{height:300, url:'/api/data'}" lay-filter="demo"> …… </table>场景二:下述方法中的键值即为基础参数项
table.render({ height: 300 ,url: '/api/data'});更多场景:下述options即为含有基础参数项的对象
> table.init('filter', options); //转化静态表格> var tableObj = table.render({}); tableObj.reload(options); //重载表格接下来看一下基础元素有哪些?
1、elem - 绑定元素是指定原始table容器,只适用于 table.render()的渲染方式
HTML:
<table id="test"></table>
JS:
table.render({ //其它参数在此省略 elem: '#test' //或 elem: document.getElementById('test') 等});2、设置表头,这里包含很多值,是一个二维数组。如果你采用表格的“方法级渲染”,那么你需要借助该参数来设定表格。如:
JS:
table.render({ cols: [[ //标题栏 {checkbox: true} ,{field: 'id', title: 'ID', width: 80} ,{field: 'username', title: '用户名', width: 120} ]]});它等价于:
<table class="layui-table" lay-data="{基础参数}" lay-filter="test"> <thead> <tr> <th lay-data="{checkbox:true}"></th> <th lay-data="{field:'id', width:80}">ID</th> <th lay-data="{field:'username', width:180}">用户名</th> </tr> </thead></table>下面是一个二级表头的例子:
JS:
table.render({ cols: [[ //标题栏 {field: 'username', title: '联系人', width: 80, rowspan: 2} //rowspan即纵向跨越的单元格数 ,{field: 'amount', title: '金额', width: 80, rowspan: 2} ,{align: 'center', title: '地址', colspan: 3} //colspan即横跨的单元格数,这种情况下不用设置field和width ], [ {field: 'province', title: '省', width: 80} ,{field: 'city', title: '市', width: 120} ,{field: 'county', title: '详细', width: 300} ]]});它等价于:
<table class="layui-table" lay-data="{基础参数}"> <thead> <tr> <th lay-data="{field:'username', width:80}" rowspan="2">联系人</th> <th lay-data="{field:'amount', width:120}" rowspan="2">金额</th> <th lay-data="{align:'center'}" colspan="3">地址</th> </tr> <tr> <th lay-data="{field:'province', width:80}">省</th> <th lay-data="{field:'city', width:120}">市</th> <th lay-data="{field:'county', width:300}">详细</th> </tr> </thead></table>需要说明的是,table模块支持无限极表头,你可按照上述的方式继续扩充。核心点在于 rowspan 和 colspan 两个参数的
接下来就是表头里的一些参数设置
<1> field:设定字段名
table.render({ cols: [[ {field: 'id'} //其它参数在此省略 ,{field: 'username'} ]]});等价于:
<th lay-data="{field:'id'}"></th><th lay-data="{field:'username'}"></th><2> title:设定标题名称
table.render({ cols: [[ {title: '邮箱'} //其它参数在此省略 ,{title: '签名'} ]]});等价于:
<th lay-data="{}">邮箱</th> (PS:也可以把标题写在lay-data里面,即 title:'邮箱')<th lay-data="{}">签名</th><3> width:设定列宽。列宽的设定也通常是必须的(“特殊列”除外,如:复选框列、工具列等),它关系到表格的整体美观程度。
table.render({ cols: [[ {width: 80} //其它参数在此省略 ,{width: 120} ]]});等价于:
<th lay-data="{width:80}"></th><th lay-data="{width:120}"></th><4> checkbox:设定复选框。如果设置 true,则表示该列内容为复选框,通常它被放在第一列。

