我正在尝试向我的 jqgrid 添加一个新行,允许用户进行内联编辑,通过单击“保存”按钮保存它,然后使用添加的数据自动重新加载网格。
我的问题是过程的重新加载部分:
jqgrid 声明:
jQuery("#gridMsg").jqGrid({
url: '@Url.Content("~\\MessagesAndDeliveryInfo\\GetMessageAndDeliveryForDecisionImplementation\\")' + '?decisionImplementationId=' + '@Model.DecisionImplementationId',
datatype: "json",
contenttype: "application/json; charset=utf-8",
mytype: "GET",
editurl: '@Url.Content("~\\MessagesAndDeliveryInfo\\SaveExistingMessage\\")',
colNames: ['Msg No.', 'Date', 'Delivery Mode', 'Delivery Number', 'Confirmation Mode', 'Confirmation Date', 'IsVoucherCreated', 'Id'],
colModel: [
{ name: 'MessageNumber', index: 'MessageNumber', width: 90 },
{ name: 'ProjectionDate', index: 'ProjectionDate', width: 90 },
{ name: 'DeliveryMode', index: 'DeliveryMode', width: 140, editable: true, edittype: "select", editoptions: { value: '@Model.DeliveryModeSelectOptions' }, formatter: showTextFmatter, unformat: unformatShowText },
{ name: 'DeliveryNumber', index: 'DeliveryNumber', width: 150, editable: true, edittype: "text" },
{ name: 'DeliveryConfirmationMode', index: 'DeliveryConfirmationMode', width: 110, align: "right", editable: true, edittype: "select", editoptions: { value: '@Model.DeliveryConfirmationModeSelectOptions' }, formatter: showTextFmatter, unformat: unformatShowText },
{ name: 'LegalDeliveryDate', index: 'LegalDeliveryDate', width: 90, align: "right", editable: true },
{ name: 'IsVoucherCreated', index: 'IsVoucherCreated', width: 90, align: "right", editable: false, hidden: true },
{ name: 'IdForSendMailService', index: 'IdForSendMailService', width: 90, align: "right", editable: false, hidden: true },
],
rowNum: 10,
rowList: [10, 20, 30],
pager: '#pagerMsg',
direction: "rtl",
viewrecords: true,
caption: "Messages",
onSelectRow: function (id) {
if (id && id != lastsel2) {
jQuery('#gridMsg').jqGrid('restoreRow', lastsel2, function (response) { debugger; });
jQuery('#gridMsg').jqGrid('editRow', id, true);
lastsel2 = id;
}
},
jsonReader: {
root: "rows",
page: "page",
total: "total",
records: "records",
repeatitems: false,
cell: "cell",
id: "id",
userdata: "userdata"
},
loadError: function (jqXHR, textStatus, errorThrown) {
debugger;
}
});
jQuery("#gridMsg").jqGrid('navGrid', '#pagerMsg', { edit: false, add: false, del: false });
添加和编辑行代码:
function AddRowToGrid()
{
rowId = 99;
var mydata = [
{ MessageNumber: rowId, ProjectionDate: '@DateTime.Now.ToShortDateString()', DeliveryMode: '@Model.OPTIONS_DEFAULT_VALUE', DeliveryNumber: "", DeliveryConfirmationMode: '@Model.OPTIONS_DEFAULT_VALUE', LegalDeliveryDate: "", IsVoucherCreated: false, IdForSendMailService: null }
];
//add row
for (var i = 0; i <= mydata.length; i++)
jQuery("#gridMsg").jqGrid('addRowData', rowId, mydata[i]);
//edit added row
jQuery("#gridMsg").jqGrid('editRow', rowId,
{
keys: true,
oneditfunc: function () {
//debugger;//alert("edited");
}
});
}
保存行代码:
function SaveNewRow(rowId)
{
var saveparameters = {
"successfunc": null,
"url": '@Url.Content("~\\MessagesAndDeliveryInfo\\SaveNewMessage\\")',
"extraparam": { decisionImpId: '@Model.DecisionImplementationId' },
"aftersavefunc": reload,
"errorfunc": function (rowId, result, postedData) { alert(result.responseText); restoreRow(rowId); },
"afterrestorefunc": null,
"restoreAfterError": true,
"mtype": "POST"
}
jQuery("#gridMsg").jqGrid('saveRow',rowId, saveparameters);
}
function reload(rowid, result) { $("#gridMsg").trigger("reloadGrid"); }
上面的代码在重新加载网格之前工作正常:即使新行保存在服务器中并且调用了 aftersavefunc:网格没有重新加载并且添加的行保持可编辑模式。只有当我在导航栏中手动单击重新加载网格时,网格才会重新加载,然后我才能看到新行。
希望有人可以帮助我,非常感谢提前。