Pravat Panda
Pravat Panda

Reputation: 1090

misleading jqgrid documentation : Client side sorting, but server side paging

this question has already been answered multiple times. but here I want to site that if the code provided in the documentation can't be achieved without additional codes, why it has been given there in first place. Its simply misleading. The code given in the documentation achieves paging, but while sorting, the grid data simply disappears.

Correct me if I am wrong.

jQuery("#gridid").jqGrid({
...
datatype: 'json', // can be xml
loadComplete : function () {
   jQuery("#gridid").jqGrid('setGridParam',{datatype:'local'});
},
onPaging : function(which_button) {
   jQuery("#gridid").jqGrid('setGridParam',{datatype:'json'});
},
...
});

Upvotes: 1

Views: 1329

Answers (2)

venugopal
venugopal

Reputation: 447

I can say there is no place where intentional misleading has happened. They are giving the pluging for free though it is a very huge plugin. And the work done by people like Oleg is making it more perfect. For you question, the code related to "client side sorting and server side paging" here is the code that can solve your problems. And this was taken with the help of some old answer given by Oleg.

This is my version of code,

  loadComplete: function(data) {

 var $this = $(this);
 if ($this.jqGrid('getGridParam', 'datatype') === 'json') {
 // because one use repeatitems: false option and uses no
 // jsonmap in the colModel the setting of data parameter
 // is very easy. We can set data parameter to data.rows:
    $this.jqGrid('setGridParam', {
      datatype: 'local',
      data: data.userdata,
      pageServer: data.page,
      recordsServer: data.records,
      lastpageServer: data.total
      });
// because we changed the value of the data parameter
// we need update internal _index parameter:
    this.refreshIndex();
      if ($this.jqGrid('getGridParam', 'sortname') !== '') {
// we need reload grid only if we use sortname parameter,
// but the server return unsorted data
    $this.triggerHandler('reloadGrid');
}
} else {
    $this.jqGrid('setGridParam', {
    page: $this.jqGrid('getGridParam', 'pageServer'),
    records: $this.jqGrid('getGridParam', 'recordsServer'),
    lastpage: $this.jqGrid('getGridParam', 'lastpageServer')
});
this.updatepager(false, true);}
}

onPaging:function(){
/*this code  is to fix the issue when we click on next page with some data in filter tool bar
     * along with this in grid definition( in filterToolbar ) we have to return true in "beforeClear "event 
     * */
    var data = $(this).jqGrid("getGridParam", "postData");
    data._search = false;
    data.filters=null;
    data.page=$(this).jqGrid("getGridParam","page");

    /* comment this line if you disable filter toolbar*/
    $(this)[0].clearToolbar();


    //Here making _search alone false will not solve problem, we have to make search also false. like in below.
    $(this).jqGrid('setGridParam', { search: false, postData:data });
    var data = $(this).jqGrid("getGridParam", "postData");


    /*this is to fix the issue when we go to last page(say there are only 3 records and page size is 5) and click 
     * on sorting the grid fetches previously loaded data (may be from its buffer) and displays 5 records  
     * where in i am expecting only 3 records to be sorted out.along with this there should be a modification in source code.
     */ 

    $(this).jqGrid("clearGridData");

    /* this is to make the grid to fetch data from server on page click*/

    $(this).setGridParam({datatype: 'json'}).triggerHandler("reloadGrid");   

}   

For the modification that you have to do in source code is , see this answer..

Upvotes: 0

Oleg
Oleg

Reputation: 221997

You don't posted the exact reference to the documentation where you get the code. I found it here.

jqGrid is open source product which you get for free. It's practical, but you should understand, that in the case the product and its documentation could not be perfect. The part of code which you referenced could work probably in some very old version of jqGrid, but it's wrong code in the current version of jqGrid. The sense of implementing "Client side sorting, but server side paging" is very suspected at all. My old answer about the subject you would find here. I would rewrite some part of the answer now, but in general the code tested with old versions could be not full compatible with the new version of jqGrid.

Upvotes: 1

Related Questions