我設法弄清楚如何去做。而不是讓jqGrid自動加載數據,需要手動執行請求,然後通過調用addJSONData
加載它。
我的jqGrid在標記定義瞭如下:
<fieldset>
<div style="display:none" class="searchResults">
<table id="eventSearchDialog-results">
</table>
<div id="eventSearchDialog-pager">
</div>
</div>
</fieldset>
我初始化用下面的代碼網格:
// 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的。沒有虛擬數據,行數據與標題不匹配。 
你對數據有什麼特別的做法?有幾個選擇,但這取決於你在做什麼。 –
在jqGrid上我有一個空白列,我將控件插入。 http://www.trirand.com/blog/jqgrid/jqgrid.html(行編輯 - >自定義編輯)中的示例在單元格數組中有一個虛擬字段,在JSON響應中,但我想動態添加虛擬字段字段添加到jqGrid看到它之前的客戶端響應,並且從服務器端發送的響應中沒有虛擬字段。 – GiddyUpHorsey
在通過jqGrid處理數據之前,沒有通用的方法來修改從服務器**返回的數據,但是如果返回的數據具有JSON格式,則可以在'jsonReader'內部以另一種格式轉換數據,或者只使用自定義格式化程序以另一種方式顯示數據。你能否在你的問題中包含定義jqGrid的JavaScript代碼和從服務器返回的完整JSON數據?你能否另外準確地描述你想要做的修改?在這個例子中,可以找到更好的解決方案。 – Oleg