operate.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <title>数据操作 - 数据表格</title>
  2. <div class="layui-card layadmin-header">
  3. <div class="layui-breadcrumb" lay-filter="breadcrumb">
  4. <a lay-href="">主页</a>
  5. <a><cite>组件</cite></a>
  6. <a><cite>数据表格</cite></a>
  7. <a><cite>数据操作</cite></a>
  8. </div>
  9. </div>
  10. <div class="layui-fluid">
  11. <div class="layui-row layui-col-space15">
  12. <div class="layui-col-md12">
  13. <div class="layui-card">
  14. <div class="layui-card-header">数据操作</div>
  15. <div class="layui-card-body">
  16. <div class="layui-btn-group test-table-operate-btn" style="margin-bottom: 10px;">
  17. <button class="layui-btn" data-type="getCheckData">获取选中行数据</button>
  18. <button class="layui-btn" data-type="getCheckLength">获取选中数目</button>
  19. <button class="layui-btn" data-type="isAll">验证是否全选</button>
  20. </div>
  21. <table class="layui-hide" id="test-table-operate" lay-filter="test-table-operate"></table>
  22. <script type="text/html" id="test-table-operate-barDemo">
  23. <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
  24. <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  25. <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
  26. </script>
  27. </div>
  28. </div>
  29. </div>
  30. </div>
  31. </div>
  32. <script>
  33. layui.use(['admin', 'table'], function(){
  34. var table = layui.table
  35. ,admin = layui.admin;
  36. table.render({
  37. elem: '#test-table-operate'
  38. ,url: './json/table/user.js'
  39. ,width: admin.screen() > 1 ? 892 : ''
  40. ,height: 332
  41. ,cols: [[
  42. {type:'checkbox', fixed: 'left'}
  43. ,{field:'id', width:80, title: 'ID', sort: true, fixed: 'left'}
  44. ,{field:'username', width:80, title: '用户名'}
  45. ,{field:'sex', width:80, title: '性别', sort: true}
  46. ,{field:'city', width:80, title: '城市'}
  47. ,{field:'sign', width: 160, title: '签名'}
  48. ,{field:'experience', width:80, title: '积分', sort: true}
  49. ,{field:'wealth', width:135, title: '财富', sort: true}
  50. ,{field:'classify', width:80, title: '职业'}
  51. ,{field:'score', width:80, title: '评分', sort: true, fixed: 'right'}
  52. ,{width:178, align:'center', fixed: 'right', toolbar: '#test-table-operate-barDemo'}
  53. ]]
  54. ,page: true
  55. });
  56. //监听表格复选框选择
  57. table.on('checkbox(test-table-operate)', function(obj){
  58. console.log(obj)
  59. });
  60. //监听工具条
  61. table.on('tool(test-table-operate)', function(obj){
  62. var data = obj.data;
  63. if(obj.event === 'detail'){
  64. layer.msg('ID:'+ data.id + ' 的查看操作');
  65. } else if(obj.event === 'del'){
  66. layer.confirm('真的删除行么', function(index){
  67. obj.del();
  68. layer.close(index);
  69. });
  70. } else if(obj.event === 'edit'){
  71. layer.alert('编辑行:<br>'+ JSON.stringify(data))
  72. }
  73. });
  74. var $ = layui.$, active = {
  75. getCheckData: function(){ //获取选中数据
  76. var checkStatus = table.checkStatus('test-table-operate')
  77. ,data = checkStatus.data;
  78. layer.alert(JSON.stringify(data));
  79. }
  80. ,getCheckLength: function(){ //获取选中数目
  81. var checkStatus = table.checkStatus('test-table-operate')
  82. ,data = checkStatus.data;
  83. layer.msg('选中了:'+ data.length + ' 个');
  84. }
  85. ,isAll: function(){ //验证是否全选
  86. var checkStatus = table.checkStatus('test-table-operate');
  87. layer.msg(checkStatus.isAll ? '全选': '未全选')
  88. }
  89. };
  90. $('.test-table-operate-btn .layui-btn').on('click', function(){
  91. var type = $(this).data('type');
  92. active[type] ? active[type].call(this) : '';
  93. });
  94. });
  95. </script>