Я пытаюсь заполнить jqGrid данными из веб-службы. Я внимательно просмотрел код и документацию jqGrid. Мне нужна еще одна пара глаз, чтобы посмотреть на приведенный ниже код и сказать, что я чего-то упускаю.
Как вы увидите в коде, у меня есть сетка, настроенная для загрузки при загрузке страницы или во время обновления . После загрузки сетки я делаю вызов Ajax, чтобы получить данные JSON (снова) и отобразить их в блоке div под сеткой.
Я вижу большую часть ожидаемого поведения. После загрузки страницы в сетке отображается индикатор загрузки, затем инициируется вызов Ajax и под сеткой отображаются данные JSON. Проблема в том, что сетка полностью пуста. Заголовки столбцов правильные, но в основной части сетки данные не отображаются.
Вот код:
$(document).ready(function () {
$('#resultDiv').html('');
$('#waitIndicator').hide();
$("#list").jqGrid({
datatype: 'json',
url: 'WeatherDataService.svc/GetWeatherData',
jsonReader: {
root: "Rows",
page: "Page",
total: "Total",
records: "Records",
repeatitems: false,
userdata: "UserData",
id: "StationId"
},
loadui: "block",
mtype: 'GET',
rowNum: 10,
rowList: [10, 20, 30],
viewrecords: true,
colNames: ['Station ID', 'Station Name', 'Timestamp', 'Max Temp',
'Min Temp', 'Precipitation', 'Snowfall', 'SnowDepth'],
colModel: [
{ name: 'StationId', index: 'StationId' },
{ name: 'StationName', index: 'StationName' },
{ name: 'Timestamp', index: 'Timestamp', align: 'right' },
{ name: 'MaxTemperature', index:'MaxTemperature',align:'right'},
{ name: 'MinTemperature', index:'MinTemperature',align:'right'},
{ name: 'Precipitation', index: 'Precipitation', align:'right'},
{ name: 'Snowfall', index: 'Snowfall', align: 'right' },
{ name: 'SnowDepth', index: 'SnowDepth', align: 'right' },
],
pager: '#pager',
sortname: 'StationId',
sortorder: 'asc',
caption: 'Weather Records',
loadComplete: function () {
// if the page index is not set (e.g. page index = 0),
// force the page index to first page
var pageIndex = $('#list').jqGrid('getGridParam', 'page');
if (pageIndex == 0) pageIndex = 1;
$('#waitIndicator').show();
$.ajax({
url: 'WeatherDataService.svc/GetWeatherData',
type: "GET",
data: ({ page: pageIndex, rows: 10,
sidx: 'StationId', sord: 'asc' }),
dataType: "json",
success: function (response) {
$('#resultDiv').html(response);
$('#waitIndicator').hide();
},
error: function (xmlHttpRequest, textStatus, errorThrown) {
$('#resultDiv').html('textStatus: ' + textStatus +
', errorThrown: ' + errorThrown);
}
});
}
});
});
Вот данные JSON из веб-службы:
{
"Total": 14975,
"Page": 1,
"Records": 149746,
"Rows": [
{
"StationId": 50130,
"StationName": "ALAMOSA WSO AP",
"Timestamp": "\/Date(725871600000)\/",
"MaxTemperature": null,
"MinTemperature": null,
"Precipitation": null,
"Snowfall": null,
"SnowDepth": null
},
{
"StationId": 50130,
"StationName": "ALAMOSA WSO AP",
"Timestamp": "\/Date(725958000000)\/",
"MaxTemperature": null,
"MinTemperature": null,
"Precipitation": null,
"Snowfall": null,
"SnowDepth": null
},
{
"StationId": 50130,
"StationName": "ALAMOSA WSO AP",
"Timestamp": "\/Date(726044400000)\/",
"MaxTemperature": null,
"MinTemperature": null,
"Precipitation": null,
"Snowfall": null,
"SnowDepth": null
},
{
"StationId": 50130,
"StationName": "ALAMOSA WSO AP",
"Timestamp": "\/Date(726130800000)\/",
"MaxTemperature": null,
"MinTemperature": null,
"Precipitation": null,
"Snowfall": null,
"SnowDepth": null
},
{
"StationId": 50130,
"StationName": "ALAMOSA WSO AP",
"Timestamp": "\/Date(726217200000)\/",
"MaxTemperature": null,
"MinTemperature": null,
"Precipitation": null,
"Snowfall": null,
"SnowDepth": null
},
{
"StationId": 50130,
"StationName": "ALAMOSA WSO AP",
"Timestamp": "\/Date(726303600000)\/",
"MaxTemperature": null,
"MinTemperature": null,
"Precipitation": null,
"Snowfall": null,
"SnowDepth": null
},
{
"StationId": 50130,
"StationName": "ALAMOSA WSO AP",
"Timestamp": "\/Date(726390000000)\/",
"MaxTemperature": null,
"MinTemperature": null,
"Precipitation": null,
"Snowfall": null,
"SnowDepth": null
},
{
"StationId": 50130,
"StationName": "ALAMOSA WSO AP",
"Timestamp": "\/Date(726476400000)\/",
"MaxTemperature": null,
"MinTemperature": null,
"Precipitation": null,
"Snowfall": null,
"SnowDepth": null
},
{
"StationId": 50130,
"StationName": "ALAMOSA WSO AP",
"Timestamp": "\/Date(726562800000)\/",
"MaxTemperature": null,
"MinTemperature": null,
"Precipitation": null,
"Snowfall": null,
"SnowDepth": null
},
{
"StationId": 50130,
"StationName": "ALAMOSA WSO AP",
"Timestamp": "\/Date(726649200000)\/",
"MaxTemperature": null,
"MinTemperature": null,
"Precipitation": null,
"Snowfall": null,
"SnowDepth": null
}
],
"UserData": null
}
Для большинства столбцов нулевые значения будут пустыми ячейками. Но я ожидаю увидеть хотя бы StationID и StationNames. Спасибо, что посмотрели.