我有一个单元格可编辑的 jqgrid,其列的编辑类型为“按钮”。单击单元格时,将出现该按钮。单击该按钮时,将出现一个模式对话框,允许用户从网格中选择一个值。一切正常。
当用户从网格中选择一个值后单击模态对话框上的“确定”按钮时,我想使用用户选择的值设置单元格值并保存单元格。
单元格被清空,而不是设置和保存单元格值。不知道为什么。
这里是相关的 jqGrid/模态对话框代码:
// global variables
base.selectedCode = null;
base.liaGridSelectedId = null;
base.liaGridSelectedICol = null;
base.liaGridSelectedIRow = null;
$("#liaGrid").jqGrid({
datatype: "local",
data: base.liaGridData,
cellEdit: true,
cellsubmit: 'clientArray',
height: 140,
colNames: ['ID', 'Class Code', 'State', 'Location Type'],
colModel: [
{ name: 'id', index: 'id', width: 90, sorttype: "int", editable: false, hidden: true },
{ name: 'ClassCode', index: 'ClassCode', width: 90, sortable: false, editable: true, edittype: "button",
editoptions: {
dataEvents: [{
type: 'click',
fn: function (e) {
e.preventDefault();
var rowid = $('#liaGrid').jqGrid('getGridParam', 'selrow');
base.liaGridSelectedId = parseInt(rowid);
$('#class-dialog').dialog('option', { width: 100, height: 200, position: 'center', title: 'Pick a Class' });
$('#class-dialog').dialog('open');
return true;
}
}]
}
},
{ name: 'LocationType', index: 'LocationType', width: 90, sortable: false, editable: true, edittype: "select", editoptions: { value: "0:;1:Rural;2:Suburban;3:Urban"} }
],
caption: "Liability Model",
beforeEditCell: function (rowid, cellname, value, iRow, iCol) {
base.liaGridSelectedICol = iCol;
base.liaGridSelectedIRow = iRow;
}
});
var infoDialog = $('#class-dialog').dialog({
autoOpen: false,
modal: true,
show: 'fade',
hide: 'fade',
resizable: true,
buttons: {
"Ok": function () {
if (base.selectedCode != null) {
$("#liaGrid").jqGrid('setCell', base.liaGridSelectedId, 'ClassCode', base.selectedCode);
$("#liaGrid").jqGrid('saveCell', base.liaGridSelectedIRow, base.liaGridSelectedICol);
$(this).dialog("close");
}
},
"Cancel": function () {
$(this).dialog("close");
}
}
});
如上所示,我尝试使用 jqGrid('setCell') 和 jqGrid('saveCell') 来更新和保存单元格的内容。不知道为什么这没有成功。
最佳答案
我让这个工作以防万一有人遇到类似的问题。我必须添加 afterSaveCell网格处理程序:
afterSaveCell: function (rowid, name, val, iRow, iCol) {
if (base.liaGridSelectedICol == 1) {
$("#liaGrid").jqGrid('setRowData', rowid, { ClassCode: base.selectedCode });
}
}
仅供引用 - base.selectedCode 在模式中设置。
奇怪的是,这只在调用 setCell 和 saveCell 方法后有效。如果没有这些在单元级别设置和保存的不成功调用,则不会调用上述处理程序。
如果有人有更合适的方法来解决这个问题,我想听听。
谢谢
关于javascript - jqGrid SetCell 和 SaveCell 在关闭模态对话框后清空单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10552706/