<!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>
文章来源地址https://www.toymoban.com/news/detail-845011.html
文章来源:https://www.toymoban.com/news/detail-845011.html
到了这里,关于easyUI点击编辑操作实现行编辑,点击取消编辑取消编辑,点击添加实现添加行操作的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!