jqgrid modify data returned from ajax call before display in table
Posted
by
user954211
on Stack Overflow
See other posts from Stack Overflow
or by user954211
Published on 2012-11-23T20:35:53Z
Indexed on
2012/11/25
11:04 UTC
Read the original article
Hit count: 163
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)
© Stack Overflow or respective owner