1

这个问题已经被多次回答了。但是在这里我想指出,如果没有附加代码就无法实现文档中提供的代码,那么为什么首先给出它。它只是误导。文档中给出的代码实现了分页,但是在排序的时候,网格数据干脆就消失了。

如果我错了,请纠正我。

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'});
},
...
});
4

2 回答 2

1

您没有发布对获取代码的文档的确切参考。我在这里找到了。

jqGrid 是您免费获得的开源产品。这很实用,但您应该明白,在这种情况下,产品及其文档可能并不完美。您引用的代码部分可能在一些非常旧的 jqGrid 版本中工作,但在当前版本的 jqGrid 中它是错误的代码。实现“客户端排序,但服务器端分页”的感觉完全令人怀疑。我关于你会在这里找到的主题的旧答案。我现在会重写部分答案,但总的来说,用旧版本测试的代码可能与新版本的 jqGrid 不完全兼容。

于 2013-05-17T09:32:11.387 回答
0

我可以说没有发生故意误导的地方。他们免费提供插件,尽管它是一个非常大的插件。像奥列格这样的人所做的工作使它变得更加完美。对于您的问题,此处与“客户端排序和服务器端分页”相关的代码是可以解决您问题的代码。这是在奥列格给出的一些旧答案的帮助下完成的。

这是我的代码版本,

  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");   

}   

对于您必须在源代码中进行的修改,请参阅此答案..

于 2013-05-20T11:10:26.123 回答