2013-05-16 53 views
1

這個問題已經被多次解答了。但在這裏我想要站點,如果在沒有附加代碼的情況下無法實現文檔中提供的代碼,爲什麼它首先在那裏給出。它只是誤導。文檔中給出的代碼實現了分頁,但是在排序時,網格數據簡單地消失。誤導jqgrid文檔:客戶端排序,但服務器端分頁

糾正我,如果我錯了。

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

回答

1

你沒有公佈準確的參考文檔,你得到的代碼。我發現它here

jqGrid是您可以免費獲得的開源產品。這是實用的,但你應該明白,在這種情況下,產品及其文件不可能是完美的。你引用的代碼部分可能在jqGrid的一些非常舊的版本中工作,但是它在當前版本的jqGrid中是錯誤的代碼。實施「客戶端排序,但服務器端分頁」的意義是非常可疑的。我對這個問題的回答可以找到here。我現在會重寫某些部分的答案,但通常用舊版本測試的代碼可能與新版本的jqGrid不完全兼容。

0

我可以說沒有故意誤導的地方。儘管它是一個非常龐大的插件,但它們可以免費使用。像奧列格這樣的人所做的工作正在使它更加完美。對於你的問題,這裏與「客戶端排序和服務器端分頁」相關的代碼是可以解決你的問題的代碼。這是奧列格給出的一些old answer的幫助。

這是我的版本的代碼,

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

} 

對於您在源代碼做的是,看到this answer修改..

+0

感謝張貼這樣一個很好的答案。我正在尋找更像3-5行代碼的更緊湊的東西。只有在網格代碼中隱含地實施它纔有可能。希望在即將到來的版本中儘快獲得一個! –