2010-08-04 15 views
0

我試圖在承載網格的頁面上使用WebMethod實現jqGrid。 ()函數中的jqGrid:如果我使用的dataType這工作jqGrid,數據類型:函數()和訪問sord,sidx等

$("#mygrid").jqGrid({ 
    ... 
    datatype: function() { 
      $.ajax({ 
       url: "myPage.aspx/gridData", 
       type: "POST", 
       contentType: "application/json; char=utf-8" 
       ... 
      }); 
    } 
)}; 

在同一頁的背後我的代碼,我有我的方法:

[WebMethod()] 
public static List<MyData> gridData() { 
    return MyClass.getData(); 
} 

我不是唯一當然可以,我可以訪問用於分頁,排序(sord,sidx等)的數據嗎?

在此先感謝。

回答

1

我建議您使用標準datatype: 'json'而不是datatype作爲功能。你只需要使用額外

datatype: 'json', 
ajaxGridOptions: { contentType: 'application/json; charset=utf-8' }, 
mtype: 'GET', 

(見Setting the content-type of requests performed by jQuery jqGrid例如)

,並返回類似以下

public class jqGridTable 
{ 
    public int total { get; set; }  // total number of pages 
    public int page { get; set; }  // current zero based page number 
    public int records { get; set; } // total number of records 
    public List<jqGridRow> rows { get; set; } 
} 
public class jqGridRow 
{ 
    public string id { get; set; } 
    public List<string> cell { get; set; } 
} 

定義jqGridTable類的實例或者,如果我們要使用從傳輸的數據中最緊湊的形式服務器到客戶端然後以下

// jsonReader: { repeatitems : true, cell:"", id: "0" } 
public class jqGridTable { 
    public int total { get; set; }   // total number of pages 
    public int page { get; set; }   // current zero based page number 
    public int records { get; set; }  // total number of records 
    public List<List<string>> rows { get; set; }// first element of row must be id 
} 

可能您應該使用其他一些jsonReader來解碼來自Web服務結果的d屬性的數據(請參閱Jqgrid 3.7 does not show rows in internet explorer)。

爲了支持服務器端分頁和排序您應該添加

int page, int rows, string sidx, string sord 

到的服務參數列表。

已更新:其他鏈接jqgrid Page 1 of x pager實際上具有jqGrid和ASMX服務的完整代碼。你可以使用以下簡單的jsonReader

jsonReader: { root: "d.rows", page: "d.page", total: "d.total", 
       records: "d.records", id: "d.names" } 
+0

我實際上遇到過你的例子。第一部分缺少「ts」定義,所以它不起作用,我不確定它是什麼。有小費嗎? 另外,如果我沒有弄錯,因爲安全性,我不能使用json的「get」。如果發佈帖子,web服務只會在json中迴應。 – bugfixr 2010-08-05 01:09:32

+0

也許你的意思是'ts' from http://stackoverflow.com/questions/2675625/setting-the-content-type-of-requests-performed-by-jquery-jqgrid/2678731#2678731。它只是來自jqGrid源代碼的代碼片段。您可以從http://www.trirand.com/blog/?page_id=6下載完整的源代碼。您可以使用JSON使用HTTP GET。關於安全性你可能意味着JSON注入。這是一個單獨討論的話題。在我使用jqGrid的主項目中,我使用HTTP GET並且有安全的解決方案。人們應該詳細解釋這一點。 – Oleg 2010-08-05 01:34:59

+0

感謝Oleg的提示。我很快就有了一個工作解決方案。 – bugfixr 2010-08-05 14:57:50