cellEvent.html 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. <blockquote class="layui-elem-quote">点击下面表格中的【签名列】,以演示单元格事件</blockquote>
  17. <table class="layui-hide" id="test-table-demoEvent" lay-filter="test-table-demoEvent"></table>
  18. </div>
  19. </div>
  20. </div>
  21. </div>
  22. </div>
  23. <script>
  24. layui.use(['admin', 'table'], function(){
  25. var table = layui.table;
  26. table.render({
  27. elem: '#test-table-demoEvent'
  28. ,height: 313
  29. ,url: './json/table/user.js'
  30. ,cols: [[
  31. {field:'id', title: 'ID', width:80}
  32. ,{field:'username', title: '用户名', width:80}
  33. ,{field:'sign', title: '签名', width:'50%', event: 'setSign', style:'cursor: pointer;'}
  34. ,{field:'experience', title: '积分'}
  35. ,{field:'score', title: '评分'}
  36. ]]
  37. });
  38. //监听单元格事件
  39. table.on('tool(test-table-demoEvent)', function(obj){
  40. var data = obj.data;
  41. if(obj.event === 'setSign'){
  42. layer.prompt({
  43. formType: 2
  44. ,title: '修改 ID 为 ['+ data.id +'] 的用户签名'
  45. ,value: data.sign
  46. }, function(value, index){
  47. layer.close(index);
  48. //这里一般是发送修改的Ajax请求
  49. //同步更新表格和缓存对应的值
  50. obj.update({
  51. sign: value
  52. });
  53. });
  54. }
  55. });
  56. });
  57. </script>