Issue displaying json data on jqgrid and formatting
I have a problem where I need to display json data to a jqgrid. The received data has the following format:
{"data":{"data":"\tat org.aaa.aaa.aaa.aaa.aaa.aaa(aaa.java:512)[147:org.aaa.aaa.aaa:9.1.1]\n\tat aaa.aaa.aaa.aaa.aaa.aaa(aaa.java:1789)[146:org.aaa:9.1.1]\n"}}
My javascript for displaying data:
$("#grid").jqGrid({
url: '/getdata',
datatype: "json",
mtype: "GET",
colNames:['data'],
colModel:[
{name:'data', index:'data', align:'center'}
],
jsonReader : {
repeatitems: false,
id: "0",
cell: "",
root: "logs",
page: function() { return 1; },
total: function() { return 1; },
records: function(obj) { return obj.length; }
},
loadonce: true,
viewrecords: true,
autowidth: true,
multiselect: false,
ignoreCase: true,
sortable: true,
height: 600,
rowNum: 999
});
I tried several combinations but couldn't get the data to be displayed on the jqgrid with this code. The jqgrid displays an empty table. I guess I'm missing something.
I also need to format the data so that every time we hit '\ n' we display it on a new line. I guess I can use "addrowdata" in the formatting for the column to do this. Is it correct?
Any pointers would be greatly appreciated.
thank,
Asha
source to share
I do not recommend using addRowData
. More efficiently used beforeProcessing
to modify the data returned from the server into a format that jqGrid can read. For example, you can split the part data.data
by \n
and populate an array of matching elements:
autoencode: true,
beforeProcessing: function (data) {
var items = data.data.data.split("\n"), i, l, item;
data.logs = [];
for (i = 0, l = items.length; i < l; i++) {
item = $.trim(items[i]);
if (item.length > 0) {
data.logs.push([item]);
}
}
}
I've included a call to jQuery.trim in the code above to remove unnecessary \t
or other whitespace characters at the beginning or end of each line.
I have included an additional autoencode: true
option that is highly recommended to ensure that the texts containing special characters to HTML (eg <
, >
, &
etc.) will be properly displayed within the grid.
Also, you must change jsonReader
to, for example, the following
jsonReader: {
root: "logs",
cell: "",
id: function () {
return function () {
return $.jgrid.randId();
}
},
page: function() { return 1; },
total: function() { return 1; },
records: function(obj) { return obj.logs.length; }
}
The meaning id
seems tricky, but the implementation generates truly unique values for the attribute id
for each row.
The related demo can be found here .
source to share