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

html 复制代码
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>EasyUI DataGrid 编辑操作示例</title>
  <!-- 引入 EasyUI 的 CSS 和 JavaScript 文件 -->
  <link rel="stylesheet" type="text/css" href="https://www.jeasyui.com/easyui/themes/default/easyui.css">
  <script type="text/javascript" src="https://www.jeasyui.com/easyui/jquery.min.js"></script>
  <script type="text/javascript" src="https://www.jeasyui.com/easyui/jquery.easyui.min.js"></script>
</head>
<body>
  <!-- 初始化 datagrid 组件 -->
<table id="datagrid"></table>

  <script>
$(function() {
  $('#datagrid').datagrid({
    data:[
  {
    "id": 1,
    "name": "张三",
    "age": 30,
    "gender": "男"
  },
  {
    "id": 2,
    "name": "李四",
    "age": 25,
    "gender": "女"
  },
  {
    "id": 3,
    "name": "王五",
    "age": 40,
    "gender": "男"
  },
  {
    "id": 4,
    "name": "赵六",
    "age": 22,
    "gender": "女"
  },
  {
    "id": 5,
    "name": "孙七",
    "age": 28,
    "gender": "男"
  }
],
    toolbar: [{
      text: '添加',
      iconCls: 'icon-edit',
      handler: function() {
		$('#datagrid').datagrid('insertRow',{
	index: 0,	// index start with 0
	row: {
		name: 'new name',
		age: 30,
		gender: '男'
	}
});
      }
    }],
    columns: [[
      {field:'id', title:'ID', width:100},
      {field:'name', title:'姓名', width:100, editor:'text'},
      {field:'age', title:'年龄', width:100, editor:'numberbox'},
      {field:'gender', title:'性别', width:100, editor:{
        type: 'combobox',
        options: {
          valueField: 'value',
          textField: 'text',
          data: [{value:'男', text:'男'},{value:'女', text:'女'}]
        }
      }},
	              {field:'action',title:'Action',width:100,
                formatter:function(value,row,index){
                    return '<a href="#" onclick="editRow('+index+')">Edit</a> '+' <a href="#" onclick="cancleRow('+index+')">cancle</a>';
                }
            }
    ]],
    onBeforeEdit: function(index, row) {
      row.editing = true;  // 标记行为编辑状态
      $('#datagrid').datagrid('refreshRow', index);  // 刷新行数据
    },
    onAfterEdit: function(index, row) {
      row.editing = false;  // 取消编辑状态
      $('#datagrid').datagrid('refreshRow', index);  // 刷新行数据
    },
    onCancelEdit: function(index, row) {
      row.editing = false;  // 取消编辑状态
      $('#datagrid').datagrid('refreshRow', index);  // 刷新行数据
    }
  });

});
  function editRow(index){
    $('#datagrid').datagrid('beginEdit', index);
}

  function cancleRow(index){
    $('#datagrid').datagrid('cancelEdit', index);
}

  </script>
</body>
</html>
相关推荐
10年前端老司机1 小时前
什么!纯前端也能识别图片中的文案、还支持100多个国家的语言
前端·javascript·vue.js
摸鱼仙人~1 小时前
React 性能优化实战指南:从理论到实践的完整攻略
前端·react.js·性能优化
程序员阿超的博客2 小时前
React动态渲染:如何用map循环渲染一个列表(List)
前端·react.js·前端框架
magic 2452 小时前
模拟 AJAX 提交 form 表单及请求头设置详解
前端·javascript·ajax
小小小小宇7 小时前
前端 Service Worker
前端
只喜欢赚钱的棉花没有糖7 小时前
http的缓存问题
前端·javascript·http
小小小小宇8 小时前
请求竞态问题统一封装
前端
loriloy8 小时前
前端资源帖
前端
源码超级联盟8 小时前
display的block和inline-block有什么区别
前端
GISer_Jing8 小时前
前端构建工具(Webpack\Vite\esbuild\Rspack)拆包能力深度解析
前端·webpack·node.js