jqgrid modify data returned from ajax call before

2019-05-10 22:23发布

问题:

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)

回答1:

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

formatter: function (cellValue) { return $.jgrid.htmlEncode(cellValue); }

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)

formatter: function (cellValue, options, rawData, action) {
    // options is the the object defined as
    //         {rowId:rowId, colModel:cm, gid:gridId, pos:colpos }
    // rawData contains the representation of the WHOLE row
    //         the most problem of the value is that it is in the same
    //         format as the input data. So if will be array of items
    //         in your case or if could be XML fragment for the row.
    //         Additional problem one will have in case of usage of
    //         loadonce:true. Ath the first load the rawData will be
    //         array of strings and on later could be named object
    //         with the properties corresponds to the column names.
    // action is typically non-important and is 'add' or 'edit'
}

For example the custom formatter of the 5-th column can be

formatter: function (cellValue, options, rawData, action) {
    switch (rawData[2]) {
    case "NO_DEVICE":
        return "***";
    case "T0_IHOME":
        return "T=" + $.jgrid.htmlEncode(cellValue) + "°C" ;
    default:
        return $.jgrid.htmlEncode(cellValue);
    }
}

One more option is the usage of beforeProcessing callback. It is mostly close tothe logic of your current code. Inside of beforeProcessing callback you can make any modifications of the data returend fron the server before the data will be processed by jqGrid.



标签: ajax jqgrid