当前位置:   article > 正文

easyUI点击编辑操作实现行编辑,点击取消编辑取消编辑,点击添加实现添加行操作

easyUI点击编辑操作实现行编辑,点击取消编辑取消编辑,点击添加实现添加行操作
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>EasyUI DataGrid 编辑操作示例</title>
  6. <!-- 引入 EasyUI 的 CSS 和 JavaScript 文件 -->
  7. <link rel="stylesheet" type="text/css" href="https://www.jeasyui.com/easyui/themes/default/easyui.css">
  8. <script type="text/javascript" src="https://www.jeasyui.com/easyui/jquery.min.js"></script>
  9. <script type="text/javascript" src="https://www.jeasyui.com/easyui/jquery.easyui.min.js"></script>
  10. </head>
  11. <body>
  12. <!-- 初始化 datagrid 组件 -->
  13. <table id="datagrid"></table>
  14. <script>
  15. $(function() {
  16. $('#datagrid').datagrid({
  17. data:[
  18. {
  19. "id": 1,
  20. "name": "张三",
  21. "age": 30,
  22. "gender": "男"
  23. },
  24. {
  25. "id": 2,
  26. "name": "李四",
  27. "age": 25,
  28. "gender": "女"
  29. },
  30. {
  31. "id": 3,
  32. "name": "王五",
  33. "age": 40,
  34. "gender": "男"
  35. },
  36. {
  37. "id": 4,
  38. "name": "赵六",
  39. "age": 22,
  40. "gender": "女"
  41. },
  42. {
  43. "id": 5,
  44. "name": "孙七",
  45. "age": 28,
  46. "gender": "男"
  47. }
  48. ],
  49. toolbar: [{
  50. text: '添加',
  51. iconCls: 'icon-edit',
  52. handler: function() {
  53. $('#datagrid').datagrid('insertRow',{
  54. index: 0, // index start with 0
  55. row: {
  56. name: 'new name',
  57. age: 30,
  58. gender: '男'
  59. }
  60. });
  61. }
  62. }],
  63. columns: [[
  64. {field:'id', title:'ID', width:100},
  65. {field:'name', title:'姓名', width:100, editor:'text'},
  66. {field:'age', title:'年龄', width:100, editor:'numberbox'},
  67. {field:'gender', title:'性别', width:100, editor:{
  68. type: 'combobox',
  69. options: {
  70. valueField: 'value',
  71. textField: 'text',
  72. data: [{value:'男', text:'男'},{value:'女', text:'女'}]
  73. }
  74. }},
  75. {field:'action',title:'Action',width:100,
  76. formatter:function(value,row,index){
  77. return '<a href="#" onclick="editRow('+index+')">Edit</a> '+' <a href="#" onclick="cancleRow('+index+')">cancle</a>';
  78. }
  79. }
  80. ]],
  81. onBeforeEdit: function(index, row) {
  82. row.editing = true; // 标记行为编辑状态
  83. $('#datagrid').datagrid('refreshRow', index); // 刷新行数据
  84. },
  85. onAfterEdit: function(index, row) {
  86. row.editing = false; // 取消编辑状态
  87. $('#datagrid').datagrid('refreshRow', index); // 刷新行数据
  88. },
  89. onCancelEdit: function(index, row) {
  90. row.editing = false; // 取消编辑状态
  91. $('#datagrid').datagrid('refreshRow', index); // 刷新行数据
  92. }
  93. });
  94. });
  95. function editRow(index){
  96. $('#datagrid').datagrid('beginEdit', index);
  97. }
  98. function cancleRow(index){
  99. $('#datagrid').datagrid('cancelEdit', index);
  100. }
  101. </script>
  102. </body>
  103. </html>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/358157
推荐阅读
相关标签
  

闽ICP备14008679号