我设法弄清楚如何去做。而不是让jqGrid自动加载数据,需要手动执行请求,然后通过调用addJSONData加载它。
我的jqGrid在标记定义了如下:
我初始化用下面的代码网格:
// Initialize the grid
this._searchResults = this._dialog.find("#eventSearchDialog-results");
this._searchResults.jqGrid(
{
datatype: "local",
colNames: ['', 'Event Name', 'Event Type', 'Start Date', 'End Date', 'Location', 'Event Country', 'Sports'],
colModel: [
{ name: 'selector', index: 'selector', width: 30 },
{ name: 'EventName', index: 'EventName', formatter: jqgridCellFormatter, width: 150 },
{ name: 'EventType', index: 'EventType', formatter: jqgridCellFormatter, width: 120 },
{ name: 'StartDate', index: 'StartDate', formatter: jqgridCellFormatter, width: 100 },
{ name: 'EndDate', index: 'EndDate', formatter: jqgridCellFormatter, width: 100 },
{ name: 'Location', index: 'Location', formatter: jqgridCellFormatter, width: 100 },
{ name: 'EventCountry', index: 'EventCountry', formatter: jqgridCellFormatter, width: 100 },
{ name: 'Sports', index: 'Sports', formatter: jqgridCellFormatter }
],
rowNum: 10,
rowList: [10, 20, 30],
pager: this._dialog.find("#eventSearchDialog-pager"),
pginput: true,
sortname: 'EventName',
viewrecords: true,
sortorder: "asc",
hidegrid: false,
height: "auto",
shrinkToFit: true,
width: 630,
jsonReader:
{
page: "pageIndex",
total: "pageCount",
records: "recordCount",
root: "rows",
repeatitems: true
},
prmNames:
{
page: "pageIndex",
rows: "pageSize",
sort: "sortField",
order: "sortOrder"
}
}
);
// Set the data type to JSON, we don't do this in the options because it will cause a request to occur,
// but we do need it to be set to JSON so that the calls to addJSONData work later.
this._searchResults.jqGrid("setGridParam", { datatype: "json" });
我从一个jQuery $.ajax()呼叫负载与数据的网格,和在success事件处理程序中,我填充数据,然后使用addJSONData将其加载到jqGrid中。
我的JSON是这样的:
{
"pageCount":1,
"pageIndex":1,
"recordCount":2,
"rows":
[
{"id":3, "cell":["Stevens Event 2", "Commonwealth Games", "03/05/2011", "16/05/2011", "sersdfweqr", "New Zealand", ["Archery"]]},
{"id":4, "cell":["Test - multiple sports", "Other", "01/05/2011", "30/06/2011", "Kobe", "Japan", ["Judo", "Karate", "Motor Sport", "Motorcycling", "Taekwondo"]]}
]
}
这是我success处理程序:
success: function (data, textStatus, xhr) {
// Pad data for our radio button column that has no corresponding field in the data
for (var counter = 0; counter < data.rows.length; counter++) {
data.rows[counter].cell.splice(0, 0, null);
}
thisEventSearchDialog._searchResults[0].addJSONData(data);
thisEventSearchDialog._createRadioButtons();
},
包含需要将假数据用于其单选按钮的列中的jqGrid的。没有虚拟数据,行数据与标题不匹配。