jqGrid,数据类型:function()以及对sord,sidx等的访问

时间:2010-08-04 19:05:53

标签: asp.net jquery jqgrid

我正在尝试使用托管网格的页面上的WebMethod来实现jqGrid。如果我在jqGrid:

中使用dataType:function(),这是有效的
 $("#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 个答案:

答案 0 :(得分: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

并返回如下所示定义的jqGridTable类实例

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; }
}

或者,如果我们想要使用从服务器传输到客户端的最紧凑形式的数据,那么

// 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来解码来自网络服务结果的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" }