2021-04-22 10:22:45 +08:00
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<html lang="en">
|
|
|
|
|
<head>
|
|
|
|
|
<meta charset="utf-8">
|
2022-08-31 23:49:29 +08:00
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
2021-04-22 10:22:45 +08:00
|
|
|
|
<title>表格操作 - layui</title>
|
|
|
|
|
|
|
|
|
|
<link rel="stylesheet" href="../src/css/layui.css">
|
|
|
|
|
<style>
|
2022-06-22 01:07:12 +08:00
|
|
|
|
body {padding: 32px; /*overflow-y: scroll;*/}
|
2021-04-22 10:22:45 +08:00
|
|
|
|
</style>
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
|
2022-05-18 22:35:13 +08:00
|
|
|
|
<div class="layui-btn-container">
|
|
|
|
|
<a href="table.html" class="layui-btn">表格综合</a>
|
2022-06-22 01:07:12 +08:00
|
|
|
|
<a href="table-test.html" class="layui-btn layui-btn-primary layui-border-green">表格操作</a>
|
2022-05-18 22:35:13 +08:00
|
|
|
|
<a href="table-static.html" class="layui-btn">静态表格</a>
|
|
|
|
|
</div>
|
|
|
|
|
|
2021-04-22 10:22:45 +08:00
|
|
|
|
<script type="text/html" id="toolbarDemo">
|
|
|
|
|
<div class="layui-btn-container">
|
|
|
|
|
<button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
|
2022-06-22 01:07:12 +08:00
|
|
|
|
<button class="layui-btn layui-btn-sm" lay-event="getData">获取当前页数据</button>
|
|
|
|
|
|
2023-02-07 08:57:03 +08:00
|
|
|
|
<button class="layui-btn layui-btn-sm" id="setRowChecked">
|
|
|
|
|
设置行选中状态
|
|
|
|
|
<i class="layui-icon layui-icon-down layui-font-12"></i>
|
|
|
|
|
</button>
|
2022-06-22 01:07:12 +08:00
|
|
|
|
<button class="layui-btn layui-btn-sm" id="reloadTest">
|
|
|
|
|
重载测试
|
|
|
|
|
<i class="layui-icon layui-icon-down layui-font-12"></i>
|
|
|
|
|
</button>
|
2023-09-04 17:23:17 +08:00
|
|
|
|
<button class="layui-btn layui-btn-sm layui-btn-primary" lay-event="multi-row"> 多行 </button>
|
|
|
|
|
<button class="layui-btn layui-btn-sm layui-btn-primary" lay-event="default-row"> 单行 </button>
|
2022-06-22 01:07:12 +08:00
|
|
|
|
<button class="layui-btn layui-btn-sm" id="moreTest">
|
|
|
|
|
更多测试
|
2021-04-22 10:22:45 +08:00
|
|
|
|
<i class="layui-icon layui-icon-down layui-font-12"></i>
|
2022-06-22 01:07:12 +08:00
|
|
|
|
</button>
|
2021-04-22 10:22:45 +08:00
|
|
|
|
</div>
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<script type="text/html" id="barDemo">
|
|
|
|
|
<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
|
2022-06-23 00:03:41 +08:00
|
|
|
|
{{# if(layui.$.trim(d.email)){ }}
|
2021-04-22 10:22:45 +08:00
|
|
|
|
<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
|
2022-05-18 22:35:13 +08:00
|
|
|
|
{{# } }}
|
2022-06-05 23:55:31 +08:00
|
|
|
|
<input type="checkbox" name="demoTableSwitch" lay-skin="switch" {{ d.demoTableSwitch }}>
|
2021-04-22 10:22:45 +08:00
|
|
|
|
</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">
|
2023-06-28 15:24:19 +08:00
|
|
|
|
<select id="demoCity1" lay-unrow lay-ignore>
|
2021-04-22 10:22:45 +08:00
|
|
|
|
<option value="浙江杭州">浙江杭州</option>
|
|
|
|
|
<option value="江西南昌">江西南昌</option>
|
|
|
|
|
<option value="湖北武汉">湖北武汉</option>
|
|
|
|
|
</select>
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<script type="text/html" id="checkboxTpl">
|
|
|
|
|
<input type="checkbox" name="" title="锁定" checked>
|
|
|
|
|
</script>
|
|
|
|
|
|
2022-06-24 10:16:50 +08:00
|
|
|
|
<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>
|
|
|
|
|
|
2022-08-24 11:27:31 +08:00
|
|
|
|
<table id="test"></table>
|
2021-04-22 10:22:45 +08:00
|
|
|
|
|
2023-09-04 17:23:17 +08:00
|
|
|
|
<script src="../src/layui.js"></script>
|
2021-04-22 10:22:45 +08:00
|
|
|
|
<script>
|
2022-06-29 08:22:26 +08:00
|
|
|
|
(function(off){
|
|
|
|
|
if(!off) return;
|
|
|
|
|
layui.disuse('table').extend({
|
2023-09-04 17:23:17 +08:00
|
|
|
|
table: '{/}https://cdn.staticfile.org/layui/2.7.6/layui.js'
|
2022-06-29 08:22:26 +08:00
|
|
|
|
});
|
|
|
|
|
})();
|
|
|
|
|
|
2021-05-18 02:42:31 +08:00
|
|
|
|
layui.use(['table', 'dropdown'], function(){
|
2022-06-22 01:07:12 +08:00
|
|
|
|
var $ = layui.$;
|
|
|
|
|
var table = layui.table;
|
|
|
|
|
var laytpl = layui.laytpl;
|
|
|
|
|
var dropdown = layui.dropdown;
|
|
|
|
|
var form = layui.form;
|
2022-06-27 00:29:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 仅用于各类测试的表头
|
|
|
|
|
var test_cols = [[
|
2023-08-15 19:54:18 +08:00
|
|
|
|
{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}
|
2022-06-27 00:29:24 +08:00
|
|
|
|
]];
|
|
|
|
|
|
2022-06-22 01:07:12 +08:00
|
|
|
|
// 全局设定某参数
|
2021-04-22 10:22:45 +08:00
|
|
|
|
table.set({
|
|
|
|
|
where: {
|
|
|
|
|
token: '默认 token 参数'
|
2023-08-15 19:54:18 +08:00
|
|
|
|
},
|
|
|
|
|
// defaultToolbar: ['filter'],
|
|
|
|
|
limit: 30,
|
|
|
|
|
// url: 'list',
|
|
|
|
|
// height: 300
|
2021-04-22 10:22:45 +08:00
|
|
|
|
});
|
|
|
|
|
|
2023-08-31 12:01:19 +08:00
|
|
|
|
// 渲染
|
2021-04-22 10:22:45 +08:00
|
|
|
|
window.ins1 = table.render({
|
2023-08-15 19:54:18 +08:00
|
|
|
|
elem: '#test',
|
|
|
|
|
height: 520,
|
|
|
|
|
// width: 600,
|
|
|
|
|
title: '用户数据表',
|
|
|
|
|
url: 'json/table/demo1.json',
|
|
|
|
|
// method: 'post',
|
2022-06-24 10:16:50 +08:00
|
|
|
|
|
2022-06-29 08:22:26 +08:00
|
|
|
|
|
2023-08-15 19:54:18 +08:00
|
|
|
|
// lineStyle: 'height: 95px;', // 行样式
|
|
|
|
|
css: [ // 自定义样式
|
|
|
|
|
'.layui-table-page{text-align: right;}',
|
|
|
|
|
'.layui-table-pagebar{float: left;}'
|
|
|
|
|
].join(''),
|
|
|
|
|
// className: '.demo-table-view',
|
2022-06-24 10:16:50 +08:00
|
|
|
|
|
2023-08-15 19:54:18 +08:00
|
|
|
|
// size: 'sm',
|
|
|
|
|
// skin: 'line',
|
|
|
|
|
// even: true,
|
|
|
|
|
// loading: false,
|
2022-06-29 08:22:26 +08:00
|
|
|
|
|
2023-08-15 19:54:18 +08:00
|
|
|
|
totalRow: true,
|
|
|
|
|
pagebar: '#pagebarDemo', // 分页栏模板
|
|
|
|
|
page: !1 ? false : {
|
|
|
|
|
// curr: layui.data('tableCache').curr || 1 // 读取记录中的页码,赋值给起始页
|
|
|
|
|
},
|
|
|
|
|
limit: 30,
|
|
|
|
|
toolbar: '#toolbarDemo',
|
|
|
|
|
defaultToolbar: ['filter', 'exports', 'print', {
|
|
|
|
|
title: '帮助',
|
|
|
|
|
layEvent: 'LAYTABLE_TIPS',
|
|
|
|
|
icon: 'layui-icon-tips'
|
|
|
|
|
}],
|
|
|
|
|
// escape: false,
|
|
|
|
|
editTrigger: 'dblclick',
|
|
|
|
|
// cellMaxWidth: 320
|
2023-09-10 20:19:42 +08:00
|
|
|
|
// cellExpandedWidth: 160, // 单元格默认展开后的宽度
|
|
|
|
|
// cellExpandedStyle: 'tips', // 单元格默认展开风格
|
2023-08-15 19:54:18 +08:00
|
|
|
|
cols: !1 ? test_cols : [[
|
|
|
|
|
{type: 'checkbox', fixed: 'left'},
|
2023-08-31 12:01:19 +08:00
|
|
|
|
// {type: 'numbers', fixed: 'left'},
|
2023-08-15 19:54:18 +08:00
|
|
|
|
{field:'id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true, totalRowText: '合计:'},
|
2023-10-14 16:25:49 +08:00
|
|
|
|
{field:'username', ignoreExport: false, expandedStyle: 'tips', title:'用户名 <i class="layui-icon layui-icon-username" lay-event="username"></i>', width:120, edit: function(d){
|
2022-07-17 11:41:00 +08:00
|
|
|
|
return !d.LAY_DISABLED;
|
2023-08-15 19:54:18 +08:00
|
|
|
|
}, templet: '#usernameTpl'},
|
|
|
|
|
{field:'xxx', title:'测试', edit: 'text'},
|
2023-08-31 12:01:19 +08:00
|
|
|
|
{field:'email', minWidth: 160, maxWidth: 320, title:'邮箱 <i class="layui-icon layui-icon-email" lay-event="email"></i>', fieldTitle: '邮箱', hide: 0, edit: 'text', style: 'background-color: #fafafa; color: red;', templet: function(d){
|
2022-07-06 00:32:53 +08:00
|
|
|
|
return '<em>'+ layui.util.escape(d.email) +'</em>'
|
2023-08-15 19:54:18 +08:00
|
|
|
|
}},
|
|
|
|
|
{field:'sex', title:'性别', width:80, edit: 'text', sort: true, escape: false},
|
|
|
|
|
{field:'city', title:'城市', width:120, templet: '#cityTpl', exportTemplet: function(d, obj){
|
2023-01-29 01:12:02 +08:00
|
|
|
|
// return '=HYPERLINK("https://abc.com/a.jpg","截图")'; // 测试内容存在特殊符
|
|
|
|
|
// console.log(obj)
|
2022-05-18 22:35:13 +08:00
|
|
|
|
var td = obj.td(this.field); //获取当前 td
|
|
|
|
|
return td.find('select').val();
|
2023-08-15 19:54:18 +08:00
|
|
|
|
}},
|
2023-09-10 20:19:42 +08:00
|
|
|
|
{field:'sign', title:'签名', minWidth: 200, style:'color: #5FB878', edit: 'textarea'},
|
2023-08-15 19:54:18 +08:00
|
|
|
|
{field: 'experience', title: '积分', width: 100, sort: true, align:'center', totalRow: !1 ? true : '{{= d.TOTAL_NUMS }} 分 😊', templet: '<div><a href="" class="layui-table-link">{{= d[d.LAY_COL.field] }}</a> 分</div>'},
|
|
|
|
|
{field:'ip', title:'IP', width: 120, align: 'right'},
|
|
|
|
|
{field:'checkin', title:'打卡', width: 100, sort: true, totalRow: '{{= d.TOTAL_ROW.checkin }} 次'},
|
|
|
|
|
{field:'joinTime', title:'加入时间', width: 120},
|
|
|
|
|
{fixed: 'right', title:'操作', toolbar: '#barDemo', width: 180}
|
|
|
|
|
]],
|
2021-04-22 10:22:45 +08:00
|
|
|
|
|
2023-08-15 19:54:18 +08:00
|
|
|
|
// autoSort: false // 禁用前端自动排序,由服务的完成排序
|
|
|
|
|
initSort111: { // 初始排序状态
|
|
|
|
|
field: 'experience', // 排序字段,对应 cols 设定的各字段名
|
|
|
|
|
type: 'desc' // 排序方式 asc: 升序、desc: 降序、null: 默认排序
|
|
|
|
|
},
|
2021-04-22 10:22:45 +08:00
|
|
|
|
|
2023-08-15 19:54:18 +08:00
|
|
|
|
headers: {headers_token: 'sasasas'},
|
|
|
|
|
where: {
|
|
|
|
|
test: '初始 test 参数',
|
|
|
|
|
token: '初始 token',
|
|
|
|
|
key: 'experience',
|
|
|
|
|
order: 'asc'
|
|
|
|
|
},
|
|
|
|
|
before: function(options){
|
2022-07-06 00:32:53 +08:00
|
|
|
|
options.where.AAAAA = 123;
|
2023-08-15 19:54:18 +08:00
|
|
|
|
// console.log(options)
|
|
|
|
|
},
|
|
|
|
|
done: function(res, curr, count){
|
2022-06-22 01:07:12 +08:00
|
|
|
|
var id = this.id;
|
|
|
|
|
|
2022-06-27 00:29:24 +08:00
|
|
|
|
// 记录当前页码
|
|
|
|
|
/*
|
|
|
|
|
layui.data('tableCache', {
|
|
|
|
|
key: 'curr',
|
|
|
|
|
value: curr
|
|
|
|
|
});
|
|
|
|
|
*/
|
|
|
|
|
|
2023-02-07 08:57:03 +08:00
|
|
|
|
// 设置选中行状态
|
|
|
|
|
dropdown.render({
|
2023-08-15 19:54:18 +08:00
|
|
|
|
elem: '#setRowChecked', // 可绑定在任意元素中,此处以上述按钮为例
|
|
|
|
|
data: [{
|
2023-02-07 08:57:03 +08:00
|
|
|
|
id: 'checked',
|
|
|
|
|
title: '选中某行'
|
|
|
|
|
},{
|
|
|
|
|
id: 'unchecked',
|
|
|
|
|
title: '取消选中某行'
|
|
|
|
|
},{
|
|
|
|
|
id: 'reset-checked',
|
|
|
|
|
title: '给选中行取消选中状态'
|
2023-08-15 19:54:18 +08:00
|
|
|
|
}],
|
|
|
|
|
// 菜单被点击的事件
|
|
|
|
|
click: function(obj){
|
2023-02-07 08:57:03 +08:00
|
|
|
|
if(obj.id === 'reset-checked'){
|
|
|
|
|
// 给选中行取消选中状态
|
|
|
|
|
table.setRowChecked(id, {
|
|
|
|
|
index: 'all',
|
|
|
|
|
checked: false
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
layer.prompt({
|
|
|
|
|
title: '请输入表格行号',
|
|
|
|
|
value: 1,
|
|
|
|
|
shade: false
|
|
|
|
|
}, function(value, i){
|
|
|
|
|
switch(obj.id){
|
|
|
|
|
case 'checked':
|
|
|
|
|
table.setRowChecked(id, {
|
2023-05-30 09:54:08 +08:00
|
|
|
|
index: value,
|
|
|
|
|
checked: true
|
2023-02-07 08:57:03 +08:00
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
case 'unchecked':
|
|
|
|
|
table.setRowChecked(id, {
|
|
|
|
|
index: value,
|
|
|
|
|
checked: false
|
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
layer.close(i);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2022-06-22 01:07:12 +08:00
|
|
|
|
// 重载测试
|
2021-05-18 02:42:31 +08:00
|
|
|
|
dropdown.render({
|
2023-09-04 17:23:17 +08:00
|
|
|
|
elem: '#reloadTest', // 可绑定在任意元素中,此处以上述按钮为例
|
2023-08-15 19:54:18 +08:00
|
|
|
|
data: [{
|
2022-06-22 01:07:12 +08:00
|
|
|
|
id: 'reload',
|
2022-06-23 00:03:41 +08:00
|
|
|
|
title: '重载'
|
2022-06-22 01:07:12 +08:00
|
|
|
|
},{
|
|
|
|
|
id: 'reload-deep',
|
2022-06-23 00:03:41 +08:00
|
|
|
|
title: '重载 - 参数叠加'
|
2022-06-22 01:07:12 +08:00
|
|
|
|
},{
|
|
|
|
|
id: 'reloadData',
|
2022-06-23 00:03:41 +08:00
|
|
|
|
title: '仅重载数据'
|
2022-06-22 01:07:12 +08:00
|
|
|
|
},{
|
|
|
|
|
id: 'reloadData-deep',
|
2022-06-23 00:03:41 +08:00
|
|
|
|
title: '仅重载数据 - 参数叠加'
|
2023-08-15 19:54:18 +08:00
|
|
|
|
}],
|
2022-06-22 01:07:12 +08:00
|
|
|
|
// 菜单被点击的事件
|
2023-08-15 19:54:18 +08:00
|
|
|
|
click: function(obj){
|
2022-06-22 01:07:12 +08:00
|
|
|
|
switch(obj.id){
|
|
|
|
|
case 'reload':
|
|
|
|
|
// 重载 - 默认(参数重置)
|
|
|
|
|
table.reload('test', {
|
|
|
|
|
where: {
|
2023-08-15 19:54:18 +08:00
|
|
|
|
abc: '123456',
|
|
|
|
|
// test: '新的 test2',
|
|
|
|
|
// token: '新的 token2'
|
|
|
|
|
},
|
|
|
|
|
height: 'full-130', // 重载高度
|
2022-06-22 01:07:12 +08:00
|
|
|
|
/*
|
2023-08-15 19:54:18 +08:00
|
|
|
|
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}
|
2022-06-22 01:07:12 +08:00
|
|
|
|
]]
|
|
|
|
|
*/
|
2022-06-23 00:03:41 +08:00
|
|
|
|
|
2022-06-22 01:07:12 +08:00
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
case 'reload-deep':
|
|
|
|
|
// 重载 - 深度(参数叠加)
|
|
|
|
|
table.reload('test', {
|
|
|
|
|
where: {
|
2023-08-15 19:54:18 +08:00
|
|
|
|
abc: 123,
|
|
|
|
|
test: '新的 test1'
|
|
|
|
|
},
|
2022-06-23 00:03:41 +08:00
|
|
|
|
//,defaultToolbar: ['print'] // 重载头部工具栏右侧图标
|
2023-08-15 19:54:18 +08:00
|
|
|
|
page: {curr: 5, limit: 20}
|
2022-06-23 00:03:41 +08:00
|
|
|
|
//,cols: ins1.config.cols
|
2022-06-22 01:07:12 +08:00
|
|
|
|
}, true);
|
|
|
|
|
break;
|
|
|
|
|
case 'reloadData':
|
|
|
|
|
// 数据重载 - 参数重置
|
2022-07-04 12:12:10 +08:00
|
|
|
|
table.reloadData('test', {
|
2022-06-22 01:07:12 +08:00
|
|
|
|
where: {
|
2023-08-15 19:54:18 +08:00
|
|
|
|
abc: '123456',
|
|
|
|
|
// test: '新的 test2',
|
|
|
|
|
// token: '新的 token2'
|
|
|
|
|
},
|
|
|
|
|
height: 2000, // 测试无效参数
|
|
|
|
|
// url: '404',
|
|
|
|
|
// elem: null,
|
|
|
|
|
// page: {curr: 5, limit: 20},
|
|
|
|
|
scrollPos: 'fixed' // 保持滚动条位置不变
|
2022-06-22 01:07:12 +08:00
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
case 'reloadData-deep':
|
|
|
|
|
// 数据重载 - 参数叠加
|
|
|
|
|
table.reloadData('test', {
|
|
|
|
|
where: {
|
2023-08-15 19:54:18 +08:00
|
|
|
|
abc: 123,
|
|
|
|
|
test: '新的 test1'
|
2022-06-22 01:07:12 +08:00
|
|
|
|
}
|
|
|
|
|
}, true);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
layer.msg('可观察 Network 请求参数的变化');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 更多测试
|
|
|
|
|
dropdown.render({
|
2023-09-04 17:23:17 +08:00
|
|
|
|
elem: '#moreTest', // 可绑定在任意元素中,此处以上述按钮为例
|
2023-08-15 19:54:18 +08:00
|
|
|
|
data: [{
|
2022-06-22 01:07:12 +08:00
|
|
|
|
id: 'add',
|
|
|
|
|
title: '添加'
|
|
|
|
|
},{
|
|
|
|
|
id: 'update',
|
|
|
|
|
title: '编辑'
|
|
|
|
|
},{
|
|
|
|
|
id: 'delete',
|
|
|
|
|
title: '删除'
|
2023-08-15 19:54:18 +08:00
|
|
|
|
}],
|
|
|
|
|
// 菜单被点击的事件
|
|
|
|
|
click: function(obj){
|
2022-06-22 01:07:12 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
2021-05-18 02:42:31 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
2023-08-15 19:54:18 +08:00
|
|
|
|
},
|
2021-04-22 10:22:45 +08:00
|
|
|
|
|
|
|
|
|
/*
|
2023-08-15 19:54:18 +08:00
|
|
|
|
request: { // 自定义请求参数名称
|
2023-09-04 17:23:17 +08:00
|
|
|
|
pageName: 'curr', // 页码的参数名称,默认:page
|
|
|
|
|
limitName: 'nums' // 每页数据量的参数名,默认:limit
|
2023-08-15 19:54:18 +08:00
|
|
|
|
},
|
|
|
|
|
parseData: function(res){ // 任意数据格式的解析
|
2021-04-22 10:22:45 +08:00
|
|
|
|
return {
|
2023-08-15 19:54:18 +08:00
|
|
|
|
"status": res.status,
|
|
|
|
|
"msg": res.message,
|
|
|
|
|
"count": res.total,
|
|
|
|
|
"data": res.data.list
|
2021-04-22 10:22:45 +08:00
|
|
|
|
};
|
2023-08-15 19:54:18 +08:00
|
|
|
|
},
|
2021-04-22 10:22:45 +08:00
|
|
|
|
*/
|
2023-08-15 19:54:18 +08:00
|
|
|
|
|
|
|
|
|
error: function(res, msg){
|
|
|
|
|
console.log(res, msg)
|
|
|
|
|
}
|
2021-04-22 10:22:45 +08:00
|
|
|
|
});
|
|
|
|
|
|
2023-09-04 17:23:17 +08:00
|
|
|
|
// 排序事件
|
2021-04-22 10:22:45 +08:00
|
|
|
|
table.on('sort(test)', function(obj){
|
2023-09-04 17:23:17 +08:00
|
|
|
|
// console.log(obj);
|
2021-04-22 10:22:45 +08:00
|
|
|
|
|
2023-09-04 17:23:17 +08:00
|
|
|
|
// return;
|
2021-04-22 10:22:45 +08:00
|
|
|
|
layer.msg('服务端排序。order by '+ obj.field + ' ' + obj.type);
|
2022-06-27 00:29:24 +08:00
|
|
|
|
|
2023-09-04 17:23:17 +08:00
|
|
|
|
// 服务端排序
|
2022-06-27 00:29:24 +08:00
|
|
|
|
table.reloadData('test', {
|
2023-09-04 17:23:17 +08:00
|
|
|
|
// initSort: obj,
|
|
|
|
|
// page: {curr: 1}, // 重新从第一页开始
|
2022-06-27 00:29:24 +08:00
|
|
|
|
where: { // 向服务端传入排序参数
|
2023-09-04 17:23:17 +08:00
|
|
|
|
key: obj.field, // 排序字段
|
|
|
|
|
order: obj.type // 排序方式
|
2021-04-22 10:22:45 +08:00
|
|
|
|
}
|
2022-06-27 00:29:24 +08:00
|
|
|
|
});
|
2021-04-22 10:22:45 +08:00
|
|
|
|
});
|
|
|
|
|
|
2023-06-20 00:03:16 +08:00
|
|
|
|
// 头部工具栏事件
|
2021-04-22 10:22:45 +08:00
|
|
|
|
table.on('toolbar(test)', function(obj){
|
2022-06-22 01:07:12 +08:00
|
|
|
|
var id = obj.config.id;
|
|
|
|
|
var checkStatus = table.checkStatus(id);
|
|
|
|
|
|
2021-04-22 10:22:45 +08:00
|
|
|
|
switch(obj.event){
|
|
|
|
|
case 'getCheckData':
|
|
|
|
|
var data = checkStatus.data;
|
2022-05-27 07:28:25 +08:00
|
|
|
|
layer.alert(layui.util.escape(JSON.stringify(data)));
|
2023-09-04 17:23:17 +08:00
|
|
|
|
break;
|
2021-04-22 10:22:45 +08:00
|
|
|
|
case 'getData':
|
2022-06-22 01:07:12 +08:00
|
|
|
|
var getData = table.getData(id);
|
2021-04-22 10:22:45 +08:00
|
|
|
|
console.log(getData);
|
2022-06-05 23:55:31 +08:00
|
|
|
|
layer.alert(layui.util.escape(JSON.stringify(getData)));
|
2023-09-04 17:23:17 +08:00
|
|
|
|
break;
|
2021-04-22 10:22:45 +08:00
|
|
|
|
case 'isAll':
|
|
|
|
|
layer.msg(checkStatus.isAll ? '全选': '未全选')
|
2023-09-04 17:23:17 +08:00
|
|
|
|
break;
|
|
|
|
|
case 'multi-row':
|
|
|
|
|
table.reload('test', {
|
|
|
|
|
// 设置行样式,此处以设置多行高度为例。若为单行,则没必要设置改参数 - 注:v2.7.0 新增
|
|
|
|
|
lineStyle: 'height: 95px;'
|
|
|
|
|
});
|
|
|
|
|
layer.msg('即通过设置 lineStyle 参数可开启多行');
|
|
|
|
|
break;
|
|
|
|
|
case 'default-row':
|
|
|
|
|
table.reload('test', {
|
|
|
|
|
lineStyle: null // 恢复单行
|
|
|
|
|
});
|
|
|
|
|
layer.msg('已设为单行');
|
|
|
|
|
break;
|
2021-04-22 10:22:45 +08:00
|
|
|
|
case 'LAYTABLE_TIPS':
|
|
|
|
|
layer.alert('Table for layui-v'+ layui.v);
|
2023-09-04 17:23:17 +08:00
|
|
|
|
break;
|
2021-04-22 10:22:45 +08:00
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2023-06-20 00:03:16 +08:00
|
|
|
|
// 表头自定义元素工具事件
|
|
|
|
|
table.on('colTool(test)', function(obj){
|
|
|
|
|
console.log(obj);
|
|
|
|
|
});
|
|
|
|
|
|
2023-09-04 17:23:17 +08:00
|
|
|
|
// 触发单元格工具事件
|
2022-06-23 00:03:41 +08:00
|
|
|
|
table.on('tool(test)', function(obj){ // 双击 toolDouble
|
2022-05-18 22:35:13 +08:00
|
|
|
|
var data = obj.data;
|
2023-09-04 17:23:17 +08:00
|
|
|
|
// console.log(obj)
|
|
|
|
|
if (obj.event === 'edit') {
|
2022-05-18 22:35:13 +08:00
|
|
|
|
layer.prompt({
|
|
|
|
|
formType: 2
|
|
|
|
|
,value: data.email
|
|
|
|
|
}, function(value, index){
|
|
|
|
|
obj.update({
|
|
|
|
|
email: value
|
2022-07-11 00:11:04 +08:00
|
|
|
|
}, true); // 参数 true 为新版新增功能,详见文档
|
|
|
|
|
|
2022-07-04 12:12:10 +08:00
|
|
|
|
/*
|
|
|
|
|
// 上述 obj.update() 只是在前端临时更新数据视图
|
|
|
|
|
// 在实际业务中,当发送修改请求成功后,可再执行 reloadData 来重载数据
|
|
|
|
|
table.reloadData('test', {
|
|
|
|
|
scrollPos: 'fixed' // 保持滚动条位置不变
|
|
|
|
|
});
|
|
|
|
|
*/
|
2022-05-18 22:35:13 +08:00
|
|
|
|
layer.close(index);
|
|
|
|
|
});
|
2023-09-04 17:23:17 +08:00
|
|
|
|
} else if (obj.event === 'del') {
|
|
|
|
|
layer.confirm('真的删除行么', function(index){
|
|
|
|
|
obj.del();
|
|
|
|
|
layer.close(index);
|
|
|
|
|
});
|
2022-05-18 22:35:13 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2023-09-04 17:23:17 +08:00
|
|
|
|
// 触发表格复选框选择
|
2022-06-22 01:07:12 +08:00
|
|
|
|
table.on('checkbox(test)', function(obj){
|
|
|
|
|
console.log(obj)
|
|
|
|
|
});
|
|
|
|
|
|
2023-09-04 17:23:17 +08:00
|
|
|
|
// 触发表格单选框选择
|
2022-06-22 01:07:12 +08:00
|
|
|
|
table.on('radio(test)', function(obj){
|
|
|
|
|
console.log(obj)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 行单击事件
|
|
|
|
|
table.on('row(test)', function(obj){
|
2023-06-20 00:03:16 +08:00
|
|
|
|
console.log(obj);
|
2023-06-12 14:59:58 +08:00
|
|
|
|
// layer.closeAll('tips');
|
|
|
|
|
obj.setRowChecked({
|
|
|
|
|
// type: 'radio'
|
|
|
|
|
});
|
|
|
|
|
// layer.msg('row 事件')
|
2022-06-22 01:07:12 +08:00
|
|
|
|
});
|
2023-06-12 14:59:58 +08:00
|
|
|
|
|
2022-06-22 01:07:12 +08:00
|
|
|
|
// 行双击事件
|
|
|
|
|
table.on('rowDouble(test)', function(obj){
|
|
|
|
|
console.log(obj);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 单元格编辑事件
|
2022-05-18 22:35:13 +08:00
|
|
|
|
table.on('edit(test)', function(obj){
|
2022-08-25 16:51:51 +08:00
|
|
|
|
var field = obj.field // 得到字段
|
|
|
|
|
var value = obj.value // 得到修改后的值
|
|
|
|
|
var oldValue = obj.oldValue // 得到修改前的值 -- v2.8.0 新增
|
|
|
|
|
var data = obj.data; // 得到当前编辑所在行的数据
|
2023-01-29 01:12:02 +08:00
|
|
|
|
var col = obj.getCol(); // 得到当前列的表头配置属性
|
2022-08-25 16:51:51 +08:00
|
|
|
|
|
|
|
|
|
// 值的校验
|
|
|
|
|
if(field === 'email'){
|
|
|
|
|
if(!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(obj.value)){
|
2023-01-29 01:12:02 +08:00
|
|
|
|
layer.tips('输入的'+ col.fieldTitle +'格式不正确,请重新编辑', this, {tips: 1});
|
2022-08-25 16:51:51 +08:00
|
|
|
|
return obj.reedit(); // 重新编辑 -- v2.8.0 新增
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 编辑后续操作,如提交更新请求,以完成真实的数据更新
|
|
|
|
|
// …
|
|
|
|
|
layer.msg('编辑成功', {icon: 1});
|
2022-05-18 22:35:13 +08:00
|
|
|
|
|
2022-08-25 16:51:51 +08:00
|
|
|
|
// 其他更新操作
|
2022-05-18 22:35:13 +08:00
|
|
|
|
var update = {};
|
|
|
|
|
update[field] = value;
|
2022-08-25 16:51:51 +08:00
|
|
|
|
obj.update(update, true); // 参数 true 为 v2.7.4 新增功能,即同步更新其他包含自定义模板并可能存在关联的列视图
|
2022-05-18 22:35:13 +08:00
|
|
|
|
});
|
2022-08-24 11:27:31 +08:00
|
|
|
|
|
2022-08-25 16:51:51 +08:00
|
|
|
|
|
2022-08-24 11:27:31 +08:00
|
|
|
|
// 列拖拽宽度后的事件 -- v2.8.0 新增
|
|
|
|
|
table.on('colResized(test)', function(obj){
|
|
|
|
|
console.log(obj);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 列拖拽宽度后的事件 -- v2.8.0 新增
|
|
|
|
|
table.on('colToggled(test)', function(obj){
|
|
|
|
|
console.log(obj);
|
|
|
|
|
});
|
2021-04-22 10:22:45 +08:00
|
|
|
|
});
|
|
|
|
|
</script>
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|