2013-02-07 28 views
2

我是jqgrid的新手,最后我设置了一个网格。假设我需要设置jsonReader,以便网格知道在json返回中我的网格数据的位置。然而,我在尝试了几天后得到了空白的细胞。jqgrid jsonReader配置

这里是我的网格:

jQuery("#list48").jqGrid({ 
      url: 'dbtest.aspx/get_offsite_history2', 
      datatype: "json", 
      mtype: 'POST', 
      ajaxGridOptions: { contentType: "application/json" }, 
      serializeGridData: function(postData) { 
       return JSON.stringify(postData); 
      }, 
      jsonReader: { 
       root: function(obj) { alert(JSON.stringify(obj.d)); return obj.d; }, 
       repeatitems: false 
      }, 
      height: 'auto', 
      rowNum: 30, 
      rowList: [10, 20, 30], 
      colNames: ['name', 'start_date', 'duration', 'offsite_cat'], 
      colModel: [ 
          { name: 'name', index: 'name', width: 80, align: 'left', editable: true, edittype: 'text' }, 
          { name: 'start_date', index: 'start_date', width: 120, align: 'left', editable: true, edittype: 'text' }, 
          { name: 'duration', index: 'duration', width: 120, align: 'left', editable: true, edittype: 'text' }, 
          { name: 'offsite_cat', index: 'offsite_cat', width: 120, align: 'left', editable: true, edittype: 'text'}], 
      pager: "#plist48", 
      viewrecords: true, 
      sortname: 'name', 
      caption: "Grouping Array Data", 
      gridview: true 
     }); 

这是从URL服务器返回dbtest.aspx/get_offsite_history2:

{"d":"[{\"name\":\"A\",\"start_date\":\"B\",\"duration\":\"C\",\"offsite_cat\":\"D\"}]"} 

我想通过设置“根得到结果: 'd' “但我得到了64空白行...

寻找评论...非常感谢

回答

3

你的问题的原因是你的服务器代码中的错误。您将序列化为JSON 两次。在对服务器响应的d属性进行反序列化之后,您仍然得到JSON字符串(!!!)而不是对象。典型的错误是在网络方法中手动使用JavaScriptSerializer.Serialize。应该返回对象本身,而不是序列化结果的字符串。

如果没有当前服务器的代码修改,你可以通过

jsonReader: { 
    root: function (obj) { 
     alert(typeof obj.d === "string" ? obj.d : JSON.stringify(obj.d)); 
     return typeof obj.d === "string" ? $.parseJSON(obj.d) : obj.d; 
    }, 
    repeatitems: false, 
    page: function() { return 1; }, 
    total: function() { return 1; }, 
    records: function (obj) { 
     return typeof obj.d === "string" ? $.parseJSON(obj.d).length : obj.length; 
    } 
} 

或使用(如果你使用loadonce: true)解决问题只是

jsonReader: { 
    root: function (obj) { 
     return typeof obj.d === "string" ? $.parseJSON(obj.d) : obj.d; 
    }, 
    repeatitems: false 
} 

因为当前的服务器代码似乎没有实现数据分页您应该增加rowNum到一些足够大的值,如rowNum: 10000或使用loadonce: true

已更新:你可以找到here修改演示,它的工作原理。它alert消息之后显示

​​

+0

你是男人!我得到了它的工作,非常感谢。 – user2045340

+0

@ user2045340:不客气! – Oleg

0

我认为问题是你返回的json数据的结构。

下面是一个我用:

{ "page":1, 
    "rows":[{"id":"1","cell":["1","1","Row 1","3","9",""]}, 
      {"id":"2","cell":["2","2","Row 2","2","1",""]}, 
      {"id":"3","cell":["3","4","Row 3","2","0",""]}], 
    "records":3, 
    "total":1 
} 

您可能需要添加一个colModel的ID来唯一地标识每一行。

例如

 colNames: ['id', 'name', 'start_date', 'duration', 'offsite_cat'], 
     colModel: [ 
         { name: 'id', index: 'id', hidden: true }, 
         { name: 'name', index: 'name', width: 80, align: 'left', editable: true, edittype: 'text' }, 
         { name: 'start_date', index: 'start_date', width: 120, align: 'left', editable: true, edittype: 'text' }, 
         { name: 'duration', index: 'duration', width: 120, align: 'left', editable: true, edittype: 'text' }, 
         { name: 'offsite_cat', index: 'offsite_cat', width: 120, align: 'left', editable: true, edittype: 'text'}], 

希望有所帮助。