2013-05-17 49 views
0

我想這是第一次加載動態列數據和列名稱從服務器,也使用服務器端分頁。動態列數據,客戶端排序和服務器分頁在jqgrid

問題是當我在jqgrid AJAX調用中使用數據類型'local'時(第二個),顯示數據並禁用服務器端分頁。但是當我在AJAX調用中使用數據類型:'json'時,沒有數據顯示。

請幫助我獲得一個簡單明瞭的解決方案,以實現動態列綁定,客戶端排序和服務器端分頁。

$(document).ready(function() { 
        $.ajax({ 
           type : "POST", 
           url : contextPath + "/getEntities", 
           dataType : "json", 
           success : function(result) { 
            var colD = result.response, 
            colM = result.colModList, 
            colN = result.colNames; 

            $("#list").jqGrid({ 
             url : contextPath + "/getEntities", 
             datatype : 'json', 
             mtype : 'POST', 

             data : colD, 
             colNames : colN, 
             colModel : colM, 
             pager : '#pager', 
             rowNum : 10, 
             rowList : [ 10, 20, 30 ], 
             sortname : 'column1', 
             sortorder : 'asc', 
             viewrecords : true, 
             gridview : true, 
             caption : 'My first grid', 
            }); 

           }, 
           loadComplete : function() { 
            $("#list").jqGrid('setGridParam',{datatype:'json'}); 
           }, 

           error : function(x, e) { 
            alert(x.readyState + " " + x.status 
              + " " + e.msg); 
           } 
          }); 
       }); 

沒有從服務器接收到的JSON數據沒有問題,因爲我能夠看到當數據類型的數據:「本地」設置(在第二AJAX調用)。如果有人提供了一個工作示例,這將是一個很大的幫助。

另一個問題是在我心中,爲什麼要重新加載網格,以及如何做到這一點。在哪裏重新加載代碼。我沒有得到這部分。

任何幫助,非常感謝。

更新:回覆。 適用於實現帶有服務器端分頁的動態列。

+0

我很困惑,爲什麼你會永遠使用'數據類型:在您的設置local'? – Mark

+0

@Mark我不想使用本地,因爲我想使用服務器端分頁加載數據。只是爲了測試我使用本地,並看到數據顯示在網格中。 –

+0

好,那麼現在讓我們完全忽略localdata,如果你這樣做了,什麼不起作用? – Mark

回答

0

工作代碼:

$(document).ready(function() { 
$.ajax({ 
     type : "GET", 
     url : contextPath + "/getEntities", 
     dataType : "json", 
     success : function(result) { 
      var colM = result.colModList, 
      colN = result.colNames; 

      $("#list").jqGrid({ 
       url : contextPath + "/getEntities", 
       datatype : 'json', 
       mtype : 'GET', 
       jsonReader : { 
        root : "response", 
        page : "page", 
        total : "total", 
        records : "records", 
        repeatitems : false, 
        id : "column1" 
       }, 
       colNames : colN, 
       colModel : colM, 
       pager : '#pager', 
       rowNum : 10, 
       rowList : [ 10, 20, 30 ], 
       sortname : 'column1', 
       sortorder : 'asc', 
       viewrecords : true, 
       gridview : true, 
       caption : 'My first grid', 
      }); 

     }, 
     error : function(x, e) { 
      alert(x.readyState + " " + x.status 
        + " " + e.msg); 
     } 
    }); 
});