easyui中编辑单个单元格
时间:2019-11-22 20:41:19|栏目:|点击: 次
在datagrid中,编辑单个单元格并实时保存。如果在datagrid中有不同的字段,控件不同,还可以将字段的field值与设定字段比较来控制改字段是否可编辑。
实现的效果为:
这是在datagrid中使用了datebox

var currentfield = undefined; var currentIndex = undefined; function onClickCell(rowIndex, field, value) { if (field != undefined) { if (currentIndex != undefined) { var ed = $('#grid').datagrid('getEditor', {index:currentIndex,field:currentfield}); var value=$(ed.target).datebox('getValue'); if(value&&value.length>0) { if (!value.match(/^((((19|20)\d{2})-(0?(1|[3-9])|1[012])-(0?[1-9]|[12]\d|30))|(((19|20)\d{2})-(0?[13578]|1[02])- 31)|(((19|20)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|((((19|20)([13579][26]|[2468][048]|0[48]))|(2000))-0?2-29))$/)) { $.messager.alert("提示", "时间格式有误,数据回滚!"); $('#grid').datagrid('rejectChanges'); coll = $('#grid').datagrid('getColumnOption', currentfield); coll.editor = null; currentIndex = undefined; currentfield = undefined; return; } } $("#grid").datagrid('endEdit', currentIndex); if (currentfield != undefined) { coll = $('#grid').datagrid('getColumnOption', currentfield); coll.editor = null; } var changes=$("#grid").datagrid("getChanges"); $('#grid').datagrid('acceptChanges'); var changedatas=JSON.stringify(changes); $.ajax({ type: 'post', dataType: 'json', data: {datas:changedatas}, url: '/FundPurchasingManagement/PurchasingExecEntering/UpdatePurchaseTaskPlan', async: false, error: function (request) { alert("Connection?error"); }, success: function (data) { if(!data.success) $.messager.alert('温馨提示', data.msg, 'info'); } }) } col = $('#grid').datagrid('getColumnOption', field); col.editor = { type: 'datebox', options: { align: 'center'} }; $("#grid").datagrid('beginEdit', rowIndex); currentIndex = rowIndex; currentfield = field; } else { currentIndex = undefined; currentfield = undefined; } }