console
$(function(){
var data={"total":28,"rows":[
{"productid":"FI-SW-01","date":"03/07/2019"},
{"productid":"K9-DL-01","date":"03/07/2019"},
{"productid":"RP-SN-01","date":"03/07/2019"},
{"productid":"RP-SN-01","date":"03/07/2019"},
{"productid":"RP-LI-02","date":"03/07/2019"},
{"productid":"FL-DSH-01","date":"03/07/2019"}
]};
$('#dg').datagrid('loadData',data);
$.extend($.fn.datagrid.methods, {
editCell: function(jq,param){
return jq.each(function(){
var opts = $(this).datagrid('options');
var fields = $(this).datagrid('getColumnFields',true).concat($(this).datagrid('getColumnFields'));
for(var i=0; i<fields.length; i++){
var col = $(this).datagrid('getColumnOption', fields[i]);
col.editor1 = col.editor;
if (fields[i] != param.field){
col.editor = null;
}
}
$(this).datagrid('beginEdit', param.index);
for(var i=0; i<fields.length; i++){
var col = $(this).datagrid('getColumnOption', fields[i]);
col.editor = col.editor1;
}
});
}
});
});
var editIndex = undefined;
var editField = undefined;
function endEditing(){
if (editIndex == undefined){return true}
if ($('#dg').datagrid('validateRow', editIndex)){
$('#dg').datagrid('endEdit', editIndex);
editIndex = undefined;
editField = undefined;
return true;
} else {
return false;
}
}
function cancelEditOrCommitChange() {
if (editIndex != undefined) {
var editor = $('#dg').datagrid('getEditor', {index:editIndex,field:editField})
var oldValue = editor.oldHtml
var newValue = editor.target[editor.type]('getValue')
if (oldValue != newValue) {
var confirmToCommit = confirm('will lose current editor content, please commit you change?')
if (confirmToCommit) {
var oldData = $('#dg').datagrid('getRows')[editIndex]
alert('ajax success')
} else {
$('#dg').datagrid('cancelEdit', editIndex)
}
}
}
endEditing()
}
function onClickCell(index, field, value){
cancelEditOrCommitChange()
var editable = index > 2;
if (!editable) { return }
if (endEditing()){
$('#dg').datagrid('selectRow', index)
.datagrid('editCell', {index:index,field:field});
editIndex = index;
editField = field
}
}
<body>
<h2>Cell Editing in DataGrid</h2>
<div class="demo-info">
<div class="demo-tip icon-tip"></div>
<div>Click a cell to start editing.</div>
</div>
<div style="margin:10px 0;"></div>
<table id="dg" class="easyui-datagrid" title="Cell Editing in DataGrid" style="width:700px;height:auto"
data-options="
iconCls: 'icon-edit',
singleSelect: true,
method:'get',
onClickCell: onClickCell
">
<thead>
<tr>
<th data-options="field:'productid',width:100">Product</th>
<th data-options="field:'date',width:250,editor:'datebox'">Date</th>
</tr>
</thead>
</table>
</body>