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>
相关推荐
Irene199113 小时前
ElementPlus 与成熟后台框架对比:vue-element-plus-admin、vue-pure-admin等
前端·ui·框架·vue3
尘中客17 小时前
放弃 Echarts?前端直接渲染后端高精度 SVG 矢量图流的踩坑记录
前端·javascript·echarts·前端开发·svg矢量图·echarts避坑
FreeBuf_18 小时前
Chrome 0Day漏洞遭野外利用
前端·chrome
小彭努力中18 小时前
199.Vue3 + OpenLayers 实现:点击 / 拖动地图播放音频
前端·vue.js·音视频·openlayers·animate
2501_9160074718 小时前
网站爬虫原理,基于浏览器点击行为还原可接口请求
前端·javascript·爬虫·ios·小程序·uni-app·iphone
前端大波18 小时前
Sentry 每日错误巡检自动化:设计思路与上手实战
前端·自动化·sentry
Highcharts.js19 小时前
适合报表系统的可视化图表|Highcharts支持直接导出PNG和PDF
javascript·数据库·react.js·pdf
ZC跨境爬虫19 小时前
使用Claude Code开发校园交友平台前端UI全记录(含架构、坑点、登录逻辑及算法)
前端·ui·架构
慧一居士19 小时前
Vue项目中,何时使用布局、子组件嵌套、插槽 对应的使用场景,和完整的使用示例
前端·vue.js
叫我一声阿雷吧19 小时前
JS 入门通关手册(35):执行上下文、调用栈与作用域链深度解析
javascript·作用域链·js进阶·执行上下文·调用栈·变量提升·闭包原理