重新加载不起作用
Reload does not work
在我的 jqGrid 中,我使用了多选参数,以便每行都有一个复选框。
网格显示一个列表,其中每一行都有一个 "Open" 状态。可以检查多行,当单击 "Close Faults from Subcontractor" 按钮时,所有勾选的条目都将分配一个新状态 "closed"。
由于此网格仅显示 "open" 个条目,因此重新查询数据库并需要显示一个新的打开条目列表。
为此,需要使用新数据重新加载网格。
目前reloadGrid不工作,显示的数据和以前一样。
$(function () {
getDataForGrid(populateGrid);
});
var populateGrid = function (data) {
var grid = $("#grid");
grid.jqGrid({
data: data,
multiselect: true,
colNames: ["Category", "Description", "Opened", "Urgency", "Location"],
colModel: [
{ name: "category", index: "category", width: 200, align: "left" },
{ name: "description", index: "description", width: 200, align: "left" },
{
name: "dateOpened",
index: "dateOpened",
width: 100,
align: "left",
formatter: "date",
formatoptions: { newformat: "d-M-Y" }
},
{ name: "urgency", label: "urgency", width: 200, align: "left" },
{ name: "location", label: "location", width: 100, align: "left" }
],
//guiStyle: "bootstrap",
prmNames: { id: "faultId" },
localReader: { id: "faultId" },
cmTemplate: { autoResizable: true },
rowNum: 20,
pager: "#pager",
shrinkToFit: true,
rownumbers: true,
sortname: "category",
viewrecords: true
}).jqGrid("gridResize")
.navGrid("#pager", {
edit: false,
add: false,
del: false,
search: false,
refresh: false
}).navButtonAdd("#pager",
{
caption: "Close Faults from Subcontractor",
buttonicon: "ui-icon-circle-check",
position: "first",
title: "Blue Button",
onClickButton: function () {
var pdfFile = $("#documentName").val();
if (pdfFile === undefined || pdfFile === null || pdfFile.length === 0) {
errorValidationMessage("You must upload a Subcontractor's confirmation pdf first before closing faults");
return false;
}
var selRowIds = grid.jqGrid("getGridParam", "selarrrow");
if (selRowIds.length === 0) {
errorValidationMessage("You did not select any faults to close");
return false;
}
var contractSubcontractorId = GetHiddenField("sir-contract-subcontractor-id");
var documentName = pdfFile.replace(/\/g, "/").replace(/.*\//, "");
var url = GetHiddenField("sir-close-faults-from-subcontractor-url");
var postData = JSON.stringify({ faultIds: selRowIds, contractSubcontractorId: contractSubcontractorId, documentName: documentName });
var callback = reloadGrid;
dataService.putData(url, postData, callback);
}
});
$("#divLoading").hide();
}
var getDataForGrid = function (callback) {
var url = GetHiddenField("sir-get-open-faults-list");
dataService.getList(url, callback);
}
var reloadGrid = function () {
showSuccessMessage("Selected Properties Closed");
getDataForGrid(reloadGridWithUpdatedData);
}
var reloadGridWithUpdatedData = function (data) {
var grid = $("#grid");
grid.jqGrid("setGridParam", { data: data });
grid.trigger("reloadGrid", data);
}
在我看来,您的问题的原因在于 setGridParam
的用法。仅当您更改不是数组的参数时,该方法才应该起作用。您当前的代码 将 旧代码 data
与新代码合并(参见 the main line of code of setGridParam
, which calls deep version of $.extend)。如果是数组,您应该使用 setGridParam
:
的第二个 (overwrite
) 参数
var reloadGridWithUpdatedData = function (data) {
var grid = $("#grid");
grid.jqGrid("setGridParam", { data: data }, true);
grid.trigger("reloadGrid");
}
或者根本不使用 setGridParam
。方法 getGridParam
为包含所有参数的内部对象提供 引用 。它允许以非常简单的方式替换(覆盖)任何参数:
var reloadGridWithUpdatedData = function (data) {
var grid = $("#grid"),
p = grid.jqGrid("getGridParam"); // get reference to all parameters
p.data = data; // replace data parameter
grid.trigger("reloadGrid");
}
在我的 jqGrid 中,我使用了多选参数,以便每行都有一个复选框。 网格显示一个列表,其中每一行都有一个 "Open" 状态。可以检查多行,当单击 "Close Faults from Subcontractor" 按钮时,所有勾选的条目都将分配一个新状态 "closed"。 由于此网格仅显示 "open" 个条目,因此重新查询数据库并需要显示一个新的打开条目列表。 为此,需要使用新数据重新加载网格。 目前reloadGrid不工作,显示的数据和以前一样。
$(function () {
getDataForGrid(populateGrid);
});
var populateGrid = function (data) {
var grid = $("#grid");
grid.jqGrid({
data: data,
multiselect: true,
colNames: ["Category", "Description", "Opened", "Urgency", "Location"],
colModel: [
{ name: "category", index: "category", width: 200, align: "left" },
{ name: "description", index: "description", width: 200, align: "left" },
{
name: "dateOpened",
index: "dateOpened",
width: 100,
align: "left",
formatter: "date",
formatoptions: { newformat: "d-M-Y" }
},
{ name: "urgency", label: "urgency", width: 200, align: "left" },
{ name: "location", label: "location", width: 100, align: "left" }
],
//guiStyle: "bootstrap",
prmNames: { id: "faultId" },
localReader: { id: "faultId" },
cmTemplate: { autoResizable: true },
rowNum: 20,
pager: "#pager",
shrinkToFit: true,
rownumbers: true,
sortname: "category",
viewrecords: true
}).jqGrid("gridResize")
.navGrid("#pager", {
edit: false,
add: false,
del: false,
search: false,
refresh: false
}).navButtonAdd("#pager",
{
caption: "Close Faults from Subcontractor",
buttonicon: "ui-icon-circle-check",
position: "first",
title: "Blue Button",
onClickButton: function () {
var pdfFile = $("#documentName").val();
if (pdfFile === undefined || pdfFile === null || pdfFile.length === 0) {
errorValidationMessage("You must upload a Subcontractor's confirmation pdf first before closing faults");
return false;
}
var selRowIds = grid.jqGrid("getGridParam", "selarrrow");
if (selRowIds.length === 0) {
errorValidationMessage("You did not select any faults to close");
return false;
}
var contractSubcontractorId = GetHiddenField("sir-contract-subcontractor-id");
var documentName = pdfFile.replace(/\/g, "/").replace(/.*\//, "");
var url = GetHiddenField("sir-close-faults-from-subcontractor-url");
var postData = JSON.stringify({ faultIds: selRowIds, contractSubcontractorId: contractSubcontractorId, documentName: documentName });
var callback = reloadGrid;
dataService.putData(url, postData, callback);
}
});
$("#divLoading").hide();
}
var getDataForGrid = function (callback) {
var url = GetHiddenField("sir-get-open-faults-list");
dataService.getList(url, callback);
}
var reloadGrid = function () {
showSuccessMessage("Selected Properties Closed");
getDataForGrid(reloadGridWithUpdatedData);
}
var reloadGridWithUpdatedData = function (data) {
var grid = $("#grid");
grid.jqGrid("setGridParam", { data: data });
grid.trigger("reloadGrid", data);
}
在我看来,您的问题的原因在于 setGridParam
的用法。仅当您更改不是数组的参数时,该方法才应该起作用。您当前的代码 将 旧代码 data
与新代码合并(参见 the main line of code of setGridParam
, which calls deep version of $.extend)。如果是数组,您应该使用 setGridParam
:
overwrite
) 参数
var reloadGridWithUpdatedData = function (data) {
var grid = $("#grid");
grid.jqGrid("setGridParam", { data: data }, true);
grid.trigger("reloadGrid");
}
或者根本不使用 setGridParam
。方法 getGridParam
为包含所有参数的内部对象提供 引用 。它允许以非常简单的方式替换(覆盖)任何参数:
var reloadGridWithUpdatedData = function (data) {
var grid = $("#grid"),
p = grid.jqGrid("getGridParam"); // get reference to all parameters
p.data = data; // replace data parameter
grid.trigger("reloadGrid");
}