I have to display some data that I receive from the server as json object like this
{"rowndx":"0","rows":"25","rowstotal":"100","rowsdata":[
["00","DEVICE001","T0_IHOME","1","***","1","10"],
["01","DEVICE002","NO_DEVICE","1","***","1","10"],
["02","DEVICE003","NO_DEVICE","0","***","1","10"],
.....
Before displaying the received data in a table I would like to make changes where necessary adding units to the numbers or replacing the numbers with words (eg 0 ->OFF 1-> ON) To do this I have associated at the ajax option "success" my encoding function. In this case, however, remains always visible the message "Loading ..." and no other action is permitted. I moved my re-encoding procedure to the "complete" ajax option and this time it seems to work. But I did not understand what was my mistake and I do not know if my procedure can work. This is my table ajax configuration
url : "devtbl.json",
mtype : "POST",
datatype : "json",
postData : ......
ajaxGridOptions: {
type : 'post',
contentType: 'application/json',
async : false,
complete : DEVparse_serverdata,
error : function() { alert('Something bad happened. Stopping');},
},
jsonReader : {
root : "tablerows",
page : "currentpage",
total : "totalpages",
records : "totalrecords",
cell : "",
id : "0",
userdata : "userdata",
repeatitems : true
},
and my coding function
function DEVparse_serverdata(js , textStatus) {
var jsontablereply = {} ;
var rowsxpage_int = parseInt(UB.rowsxpage.DEVtable) ;
var jsonreply = jQuery.parseJSON(js.responseText) ;
jsontablereply.currentpage = "" + (1 + (parseInt(jsonreply.rowndx) / rowsxpage_int));
jsontablereply.totalpages = "" + parseInt((parseInt(jsonreply.rowstotal) + (rowsxpage_int-1)) / rowsxpage_int) ;
jsontablereply.totalrecords = jsonreply.rowstotal;
jsontablereply.tablerows = [] ;
$.each(jsonreply.rowsdata, function(ndx, row) {
var rowarray = [] ;
rowarray[0] = row[0] ;
rowarray[1] = row[1] ;
rowarray[2] = row[2] ;
rowarray[3] = row[3] ;
rowarray[4] = row[4] ;
switch (row[2]) {
case "NO_DEVICE":
rowarray[5] = "***" ;
break ;
case "T0_IHOME":
rowarray[5] = "T=" + row[5] + "°C" ;
break ;
}
jsontablereply.tablerows[ndx] = rowarray ;
}) ; // each
jQuery("#DEVtbl")[0].addJSONData(jsontablereply);
}
(I am a beginner with Jquery my coding style is poor)
There are many possibilities which you have to implement your requirements.
In many case one can use predefined formatter: "select" or formatter: "checkbox" for the case
0 ->OFF 1-> ON
.Another possibility is the usage of custom formatter and unformatter. The custom formatter is just callback which will be used by jqGrid during building of HTML fragment of cells of the corresponding column. If you need common displaying of some text the formatter will look like
The advantage of custom formatter is that you can not only many any modification of the source text, but you can build the cell contain based of information from other columns (see
rawData
parameter below)For example the custom formatter of the 5-th column can be
One more option is the usage of
beforeProcessing
callback. It is mostly close tothe logic of your current code. Inside ofbeforeProcessing
callback you can make any modifications of the data returend fron the server before the data will be processed by jqGrid.