【问题标题】:How i can solve problems with jqGrid sorting, paging?我如何解决 jqGrid 排序、分页的问题?
【发布时间】:2014-02-05 08:36:26
【问题描述】:

好的,我将服务器端代码更改为

  int nm = objects.ToList().Count;
  if (objects.ToList().Count > 0)
       return new PagedList(objects, nm, 1, 25, null);
  else return null;

json 改为

{"d":{"total":15,"page":1,"records":366,"rows":[{"id":"34324","LastDateChange":"/Date(1391464800000 )/","DateLoad":"/Date(1391464800000)/"....,"AName":"fg"}],"userData":null}}

客户端

   $("#table").jqGrid({
    url: '/WebSrv.asmx/GetSaleObjects',
    datatype: 'json',
    mtype: 'POST',
    loadonce: true,
   ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
   serializeGridData: function (postData) {
   },
   viewrecords: true,
   sortable: true,
   gridview: true,
   multisort: true,
   height: 'auto',
   width: 'auto',
   pager: "#pgrf",
   autowidth: true,
   pagination: true,
    jsonReader: {
       root: "d.rows",
       page: "d.page",
       total: "d.total",
       records: "d.records"
   }
 ....
   }); 

但是网格是空的!不明白;(,可能是样本?

【问题讨论】:

    标签: c# javascript jquery asp.net jqgrid


    【解决方案1】:

    您似乎得到了非常古老的示例作为解决方案的模板。例如,选项imgpath 已在版本 3.5 中被弃用(请参阅the documentation)。所以你使用了为 jqGrid 至少 5 年的旧版本创建的模板。

    您的代码中的另一个重要错误是在 ASMX Web 服务方法中使用手动 JSON 序列化。您应该从GetSaleObjects 中删除对Newtonsoft.Json.JsonConvert.SerializeObject 的调用。该方法应返回PagedList 对象或仅返回Object。顺便说一句,如果使用loadonce: true,您可以返回objects(或objects.ToList())而不是new PagedList(objects, objects.ToList().Count, 1, objects.ToList().Count),因为jqGrid 忽略page、records和total部分loadonce: true 情况下的服务器响应。

    了解当前代码将结果序列化为 JSON 两次 很重要。所以你必须在success 的$.ajax 的句柄内调用JSON.parse。例如,如果您有一个 new PagedList(...) 对象,那么第一个序列化为 JSON 会产生类似

    的字符串
    {"total":1,"page":1,"records":350,"rows":[...]}
    

    ASP.NET 序列化另外返回的结果。所以将返回值修改为字符串

    "{\"total\":1,\"page\":1,\"records\":350,\"rows\":[...]}"
    

    正确的 JSON 字符串 ({"total":1,"page":1,"records":350,"rows":[...]}) 的反序列化将在客户端生成具有 total、rows 等属性的对象。另一方面,对错误字符串 ({\"total\":1,\"page\":1,\"records\":350,\"rows\":[...]}) 的反序列化会生成包含文本 {"total":1,"page":1,"records":350,"rows":[...]} 的 字符串。只有当一个应用额外调用jQuery.parseJSON 或JSON.parse 时,一个才会获得必需的object。

    【讨论】:

      猜你喜欢
      • 2021-07-26
      • 2017-07-13
      • 2021-06-30
      • 1970-01-01
      • 2015-03-20
      • 1970-01-01
      • 2011-04-17
      • 2013-04-04
      相关资源
      最近更新 更多