Estoy intentando llenar un jqGrid con datos de un servicio web. He revisado a fondo el código jqGrid y la documentación. Necesito otro par de ojos para mirar el siguiente código y decirme si me falta algo.jqGrid no se llena con datos
Como se verá en el código, tengo la red creada para carga cuando se carga la página o durante una actualización. Después de cargar la grilla, hago una llamada Ajax para obtener los datos JSON (nuevamente) y mostrarlos en un div debajo de la grilla.
que ver la mayor parte del comportamiento esperado. Después de cargar la página, la cuadrícula muestra el indicador de carga, luego se inicia la llamada Ajax y los datos JSON se muestran debajo de la cuadrícula. El problema es que la grilla está completamente vacía. Los encabezados de las columnas son correctos, pero no aparecen datos en el cuerpo de la cuadrícula.
Aquí está el código:
$(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);
}
});
}
});
});
Estos son los datos JSON desde el servicio web:
{
"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
}
Para la mayoría de las columnas de los valores nulos serán resultado es celdas vacías. Pero espero ver al menos los StationID y StationNames. Gracias por echar un vistazo.
Gracias por su respuesta, Oleg. Lamentablemente, todavía no puedo cargar la red después de implementar sus sugerencias. Cambié la información de la fecha al formato ISO y agregué "formateador: 'fecha'" a las opciones de jqGrid. También agregué las propiedades ajaxGridOptions y serializeRowData. Cuando almaceno los datos JSON en un archivo y cambio la propiedad url para que apunte al archivo, la grilla se carga. Esto me hace preguntar si el problema es con la llamada ajax y el servicio web. La firma del método se muestra en el siguiente comentario. –
[WebGet (ResponseFormat = WebMessageFormat.Json, bodystyle = WebMessageBodyStyle.Bare)] \t [OperationContract] cadena pública GetWeatherData (página int, int filas, SIDX cadena, Sord cadena) { // Código para obtener los datos JavaScriptSerializer jsSerializer = new JavaScriptSerializer(); cadena jsonOutput = jsSerializer.Serialize (DataOutput); return jsonOutput; \t}} –
¿Qué versión de .NET utiliza? ¿Usas Visual Studio 2008 o 2010? Si responde las preguntas, podría publicar un ejemplo de código pequeño correspondiente. – Oleg