2010-08-04 111 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