438 lines
14 KiB
HTML
438 lines
14 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
|
||
<title>表格操作 - layui</title>
|
||
|
||
<link rel="stylesheet" href="../src/css/layui.css">
|
||
<style>
|
||
body {padding: 32px; /*overflow-y: scroll;*/}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
|
||
<div class="layui-btn-container">
|
||
<a href="table.html" class="layui-btn">表格综合</a>
|
||
<a href="table-test.html" class="layui-btn layui-btn-primary layui-border-green">表格操作</a>
|
||
<a href="table-static.html" class="layui-btn">静态表格</a>
|
||
</div>
|
||
|
||
<script type="text/html" id="toolbarDemo">
|
||
<div class="layui-btn-container">
|
||
<button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
|
||
<button class="layui-btn layui-btn-sm" lay-event="getData">获取当前页数据</button>
|
||
<button class="layui-btn layui-btn-sm" lay-event="isAll">是否全选</button>
|
||
|
||
<button class="layui-btn layui-btn-sm" id="reloadTest">
|
||
重载测试
|
||
<i class="layui-icon layui-icon-down layui-font-12"></i>
|
||
</button>
|
||
<button class="layui-btn layui-btn-sm" id="moreTest">
|
||
更多测试
|
||
<i class="layui-icon layui-icon-down layui-font-12"></i>
|
||
</button>
|
||
</div>
|
||
</script>
|
||
|
||
<script type="text/html" id="barDemo">
|
||
<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
|
||
{{# if(layui.$.trim(d.email)){ }}
|
||
<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
|
||
{{# } }}
|
||
<input type="checkbox" name="demoTableSwitch" lay-skin="switch" {{ d.demoTableSwitch }}>
|
||
</script>
|
||
|
||
<script type="text/html" id="usernameTpl">
|
||
<a href="" class="layui-table-link">{{d.username || ''}}</a>
|
||
</script>
|
||
|
||
<script type="text/html" id="switchTpl">
|
||
<input type="checkbox" name="yyy" lay-skin="switch" lay-text="女|男">
|
||
</script>
|
||
|
||
<script type="text/html" id="cityTpl">
|
||
<select id="demoCity1" lay-ignore>
|
||
<option value="浙江杭州">浙江杭州</option>
|
||
<option value="江西南昌">江西南昌</option>
|
||
<option value="湖北武汉">湖北武汉</option>
|
||
</select>
|
||
</script>
|
||
|
||
<script type="text/html" id="checkboxTpl">
|
||
<input type="checkbox" name="" title="锁定" checked>
|
||
</script>
|
||
|
||
<script type="text/html" id="LAY_table_tpl_email">
|
||
<span {{# if(!d.activate){ }}style="color:#999"{{# } }}>{{ d.email }}</span>
|
||
</script>
|
||
|
||
<script type="text/html" id="pagebarDemo">
|
||
<div class="layui-btn-container">
|
||
<button class="layui-btn layui-btn-sm" lay-event="footerDemoBtn1">底部按钮1</button>
|
||
<button class="layui-btn layui-btn-sm" lay-event="footerDemoBtn2">底部按钮2</button>
|
||
</div>
|
||
</script>
|
||
|
||
<table id="test" lay-filter="test"></table>
|
||
|
||
<script src="../src/layui.js"></script>
|
||
<script>
|
||
layui.use(['table', 'dropdown'], function(){
|
||
var $ = layui.$;
|
||
var table = layui.table;
|
||
var laytpl = layui.laytpl;
|
||
var dropdown = layui.dropdown;
|
||
var form = layui.form;
|
||
|
||
|
||
// 仅用于各类测试的表头
|
||
var test_cols = [[
|
||
{type: 'checkbox', fixed: 'left'}
|
||
,{field:'id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true, totalRowText: '合计:'}
|
||
,{field:'username', title:'用户名', width:120, edit: 'text'}
|
||
,{field:'email', title:'邮箱 <i class="layui-icon layui-icon-email"></i>', hide: 0, width:150, edit: 'text'}
|
||
,{field:'sex', title:'性别', width:80, edit: 'text', sort: true}
|
||
,{field:'city', title:'城市', width: 120}
|
||
,{field:'sign', title:'签名'}
|
||
,{field: 'experience', title: '积分', width:80, sort: true, align:'center', totalRow: '{{ d.TOTAL_NUMS }} 😊'}
|
||
,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
|
||
]];
|
||
|
||
// 全局设定某参数
|
||
table.set({
|
||
where: {
|
||
token: '默认 token 参数'
|
||
}
|
||
//,defaultToolbar: ['filter']
|
||
,limit: 30
|
||
//,url: 'list'
|
||
//,height: 300
|
||
});
|
||
|
||
//渲染
|
||
window.ins1 = table.render({
|
||
elem: '#test'
|
||
,height: 500
|
||
//,width: 600
|
||
,title: '用户数据表'
|
||
,url: 'json/table/demo1.json'
|
||
|
||
,pagebar: '#pagebarDemo' // 分页栏模板
|
||
,lineStyle: 'height: 95px;' // 行样式
|
||
,css: [ // 自定义样式
|
||
'.layui-table-page{text-align: right;}'
|
||
,'.layui-table-pagebar{float: left;}'
|
||
].join('')
|
||
|
||
//,size: 'lg'
|
||
//,skin: 'line'
|
||
//,autoSort: false //是否自动排序。如果否,则由服务端排序
|
||
//,loading: false
|
||
,totalRow: true
|
||
,page: {
|
||
// curr: layui.data('tableCache').curr || 1 // 读取记录中的页码,赋值给起始页
|
||
}
|
||
,limit: 30
|
||
,toolbar: '#toolbarDemo'
|
||
,defaultToolbar: ['filter', 'exports', 'print', {
|
||
title: '帮助'
|
||
,layEvent: 'LAYTABLE_TIPS'
|
||
,icon: 'layui-icon-tips'
|
||
}]
|
||
//,escape: false
|
||
,cols: !1 ? test_cols : [[
|
||
{type: 'checkbox', fixed: 'left'}
|
||
,{field:'id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true, totalRowText: '合计:'}
|
||
,{field:'username', title:'用户名', width:120, edit: 'text', templet: '#usernameTpl'}
|
||
,{field:'email', title:'邮箱 <i class="layui-icon layui-icon-email"></i>', hide: 0, width:150, edit: 'text', templet: function(d){
|
||
return '<em>'+ layui.util.escape(d.email) +'</em>'
|
||
}}
|
||
,{field:'sex', title:'性别', width:80, edit: 'text', sort: true}
|
||
,{field:'city', title:'城市', width:120, templet: '#cityTpl', exportTemplet: function(d, obj){
|
||
//console.log(obj)
|
||
var td = obj.td(this.field); //获取当前 td
|
||
return td.find('select').val();
|
||
}}
|
||
,{field:'sign', title:'签名', minWidth: 200, style:'color: #5FB878', edit: 'textarea'}
|
||
,{field: 'experience', title: '积分', width: 100, sort: true, align:'center', totalRow: !1 || '{{= d.TOTAL_NUMS }} 分 😊', templet: '<div><a href="" class="layui-table-link">{{ d.experience }}</a> 分</div>'}
|
||
,{field:'ip', title:'IP', width: 120, align: 'right'}
|
||
,{field:'checkin', title:'打卡', width: 100, sort: true, totalRow: '{{= parseInt(d.TOTAL_NUMS) }} 次'}
|
||
,{field:'joinTime', title:'加入时间', width: 120}
|
||
,{fixed: 'right', title:'操作', toolbar: '#barDemo', width: 180}
|
||
]]
|
||
|
||
//,autoSort: false // 禁用前端自动排序
|
||
,initSort11111: { // 初始排序状态
|
||
field: 'experience' //排序字段,对应 cols 设定的各字段名
|
||
,type: 'desc' //排序方式 asc: 升序、desc: 降序、null: 默认排序
|
||
}
|
||
|
||
,headers: {headers_token: 'sasasas'}
|
||
,where: {
|
||
test: '初始 test 参数'
|
||
,token: '初始 token'
|
||
,key: 'experience'
|
||
,order: 'asc'
|
||
}
|
||
,done: function(res, curr, count){
|
||
var id = this.id;
|
||
|
||
// 记录当前页码
|
||
/*
|
||
layui.data('tableCache', {
|
||
key: 'curr',
|
||
value: curr
|
||
});
|
||
*/
|
||
|
||
// 重载测试
|
||
dropdown.render({
|
||
elem: '#reloadTest' //可绑定在任意元素中,此处以上述按钮为例
|
||
,data: [{
|
||
id: 'reload',
|
||
title: '重载'
|
||
},{
|
||
id: 'reload-deep',
|
||
title: '重载 - 参数叠加'
|
||
},{
|
||
id: 'reloadData',
|
||
title: '仅重载数据'
|
||
},{
|
||
id: 'reloadData-deep',
|
||
title: '仅重载数据 - 参数叠加'
|
||
}]
|
||
// 菜单被点击的事件
|
||
,click: function(obj){
|
||
switch(obj.id){
|
||
case 'reload':
|
||
// 重载 - 默认(参数重置)
|
||
table.reload('test', {
|
||
where: {
|
||
abc: '123456'
|
||
//,test: '新的 test2'
|
||
//,token: '新的 token2'
|
||
}
|
||
,height: 'full-130' // 重载高度
|
||
/*
|
||
,cols: [[ // 重置表头
|
||
{type: 'checkbox', fixed: 'left'}
|
||
,{field:'id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true, totalRowText: '合计:'}
|
||
,{field:'sex', title:'性别', width:80, edit: 'text', sort: true}
|
||
,{field:'experience', title:'积分', width:80, sort: true, totalRow: true, templet: '<div>{{ d.experience }} 分</div>'}
|
||
,{field:'logins', title:'登入次数', width:100, sort: true, totalRow: true}
|
||
,{field:'joinTime', title:'加入时间', width:120}
|
||
]]
|
||
*/
|
||
|
||
});
|
||
break;
|
||
case 'reload-deep':
|
||
// 重载 - 深度(参数叠加)
|
||
table.reload('test', {
|
||
where: {
|
||
abc: 123
|
||
,test: '新的 test1'
|
||
}
|
||
//,defaultToolbar: ['print'] // 重载头部工具栏右侧图标
|
||
,page: {curr: 5, limit: 20}
|
||
//,cols: ins1.config.cols
|
||
}, true);
|
||
break;
|
||
case 'reloadData':
|
||
// 数据重载 - 参数重置
|
||
var isnt3 = table.reloadData('test', {
|
||
where: {
|
||
abc: '123456'
|
||
//,test: '新的 test2'
|
||
//,token: '新的 token2'
|
||
}
|
||
,height: 2000 // 测试无效参数
|
||
//,url: '404'
|
||
,elem: null
|
||
,page: {curr: 5, limit: 20}
|
||
});
|
||
console.log(isnt3.config);
|
||
break;
|
||
case 'reloadData-deep':
|
||
// 数据重载 - 参数叠加
|
||
table.reloadData('test', {
|
||
where: {
|
||
abc: 123
|
||
,test: '新的 test1'
|
||
}
|
||
}, true);
|
||
break;
|
||
}
|
||
layer.msg('可观察 Network 请求参数的变化');
|
||
}
|
||
});
|
||
|
||
|
||
// 更多测试
|
||
dropdown.render({
|
||
elem: '#moreTest' //可绑定在任意元素中,此处以上述按钮为例
|
||
,data: [{
|
||
id: 'add',
|
||
title: '添加'
|
||
},{
|
||
id: 'update',
|
||
title: '编辑'
|
||
},{
|
||
id: 'delete',
|
||
title: '删除'
|
||
}]
|
||
//菜单被点击的事件
|
||
,click: function(obj){
|
||
var checkStatus = table.checkStatus(id)
|
||
var data = checkStatus.data; // 获取选中的数据
|
||
|
||
switch(obj.id){
|
||
case 'add':
|
||
layer.open({
|
||
title: '添加',
|
||
type: 1,
|
||
area: ['80%','80%'],
|
||
content: '<div style="padding: 16px;">自定义表单元素</div>'
|
||
});
|
||
break;
|
||
case 'update':
|
||
layer.open({
|
||
title: '编辑',
|
||
type: 1,
|
||
area: ['80%','80%'],
|
||
content: '<div style="padding: 16px;">自定义表单元素</div>'
|
||
});
|
||
break;
|
||
case 'delete':
|
||
if(data.length === 0){
|
||
return layer.msg('请选择一行');
|
||
}
|
||
layer.msg('delete event');
|
||
break;
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
,error: function(res, msg){
|
||
console.log(res, msg)
|
||
}
|
||
|
||
/*
|
||
,request: { // 自定义请求参数名称
|
||
pageName: 'curr' //页码的参数名称,默认:page
|
||
,limitName: 'nums' //每页数据量的参数名,默认:limit
|
||
}
|
||
,parseData: function(res){ // 任意数据格式的解析
|
||
return {
|
||
"status": res.status
|
||
,"msg": res.message
|
||
,"count": res.total
|
||
,"data": res.data.list
|
||
};
|
||
}
|
||
*/
|
||
});
|
||
|
||
//排序事件
|
||
table.on('sort(test)', function(obj){
|
||
//console.log(obj);
|
||
|
||
//return;
|
||
layer.msg('服务端排序。order by '+ obj.field + ' ' + obj.type);
|
||
|
||
//服务端排序
|
||
table.reloadData('test', {
|
||
//initSort: obj,
|
||
//page: {curr: 1}, //重新从第一页开始
|
||
where: { // 向服务端传入排序参数
|
||
key: obj.field, //排序字段
|
||
order: obj.type //排序方式
|
||
}
|
||
});
|
||
});
|
||
|
||
// 工具栏事件
|
||
table.on('toolbar(test)', function(obj){
|
||
var id = obj.config.id;
|
||
var checkStatus = table.checkStatus(id);
|
||
|
||
switch(obj.event){
|
||
case 'getCheckData':
|
||
var data = checkStatus.data;
|
||
layer.alert(layui.util.escape(JSON.stringify(data)));
|
||
break;
|
||
case 'getData':
|
||
var getData = table.getData(id);
|
||
console.log(getData);
|
||
layer.alert(layui.util.escape(JSON.stringify(getData)));
|
||
break;
|
||
case 'isAll':
|
||
layer.msg(checkStatus.isAll ? '全选': '未全选')
|
||
break;
|
||
case 'LAYTABLE_TIPS':
|
||
layer.alert('Table for layui-v'+ layui.v);
|
||
break;
|
||
};
|
||
});
|
||
|
||
//触发单元格工具事件
|
||
table.on('tool(test)', function(obj){ // 双击 toolDouble
|
||
var data = obj.data;
|
||
//console.log(obj)
|
||
if(obj.event === 'del'){
|
||
layer.confirm('真的删除行么', function(index){
|
||
obj.del();
|
||
layer.close(index);
|
||
});
|
||
} else if(obj.event === 'edit'){
|
||
layer.prompt({
|
||
formType: 2
|
||
,value: data.email
|
||
}, function(value, index){
|
||
obj.update({
|
||
email: value
|
||
});
|
||
layer.close(index);
|
||
});
|
||
}
|
||
});
|
||
|
||
|
||
|
||
//触发表格复选框选择
|
||
table.on('checkbox(test)', function(obj){
|
||
console.log(obj)
|
||
});
|
||
|
||
//触发表格单选框选择
|
||
table.on('radio(test)', function(obj){
|
||
console.log(obj)
|
||
});
|
||
|
||
// 行单击事件
|
||
table.on('row(test)', function(obj){
|
||
//console.log(obj);
|
||
//layer.closeAll('tips');
|
||
});
|
||
// 行双击事件
|
||
table.on('rowDouble(test)', function(obj){
|
||
console.log(obj);
|
||
});
|
||
|
||
// 单元格编辑事件
|
||
table.on('edit(test)', function(obj){
|
||
var field = obj.field //得到字段
|
||
,value = obj.value //得到修改后的值
|
||
,data = obj.data; //得到所在行所有键值
|
||
|
||
var update = {};
|
||
update[field] = value;
|
||
obj.update(update);
|
||
});
|
||
});
|
||
</script>
|
||
</body>
|
||
</html>
|