2014-02-05 19 views
0

确定我改变服务器端代码如何解决jqGrid排序,分页问题?

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

JSON是变化到

{ “d”:{ “总”:15, “页”:1, “记录”:366,“行“:[{” ID “:” 34324" , “LastDateChange”: “/日期(1391464800000)/”, “DateLoad”: “/日期(1391464800000)/” ..., “AName”: “FG”} ], “用户数据”:空}}

客户端

$("#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" 
    } 
.... 
    }); 

但网格是空的! `吨uderstand;(可能是样品

回答

0

看来你非常非常老的例子作为模板解决方案,例如选项imgpath已被弃用(见the documentation)已经在3.5版本,所以你用吗?。在执法机关5张赞成票旧版本的jqGrid创建模板。

在你的代码的另一个重要缺陷是手动的JSON序列化的ASMX Web服务方法内的使用情况。你应该从GetSaleObjects去除Newtonsoft.Json.JsonConvert.SerializeObject调用该方法应该返回PagedList对象或只是Object。顺便说一句,您可以返回objects(或objects.ToList())而不是new PagedList(objects, objects.ToList().Count, 1, objects.ToList().Count)美国ge loadonce: true,因为jqGrid 忽略了page,recordstotal服务器响应的部分情况为loadonce: true

理解当前代码将结果序列化为JSON 两次是很重要的。所以你必须拨打里面success$.ajax的句柄。例如,如果你有一个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":[...]})会产生与在客户端属性totalrows等的对象。另一方面,错误字符串的反序列化({\"total\":1,\"page\":1,\"records\":350,\"rows\":[...]})产生字符串其中包含文本{"total":1,"page":1,"records":350,"rows":[...]}。只有当您应用jQuery.parseJSONJSON.parse的额外呼叫时,才会获得要求对象