2012-01-30 59 views
0

你好,我有一個正常工作的例子:的jqGrid JSON數據

$(function() { 
     $("#treegrid").jqGrid({ 
      url: 'tree2.json', 
      datatype: 'json', 
      mtype: 'GET', 
      colNames: ["ID", "Description", "Total"], 
      colModel: [{ 
       name: 'id', 
       index: 'id', 
       width: 1, 
       hidden: true, 
       key: true 
      }, { 
       name: 'desc', 
       index: 'desc', 
       hidden: false, 
       sortable: true 
      }, { 
       name: 'num', 
       index: 'num', 
       hidden: false, 
       sortable: true 
      }], 
      height: 'auto', 
      width: '500', 
      pager: "#ptreegrid", 
      caption: "Tree Grid Example" 
     }) 
    }); 

JSON數據

{ 
    "page": 1, 
    "total": 1, 
    "records": 2, 
    "rows": [ 
     {"id": 1, "cell": ["1", "Source 1", "300"]}, 
     {"id": 2, "cell": ["2", "Source 2", "100"]} 
    ] 
} 

如何訂購的jqGrid閱讀這種類型的JSON數據?可能嗎?

{ 
    "page": 1, 
    "total": 1, 
    "records": 2, 
    "rows": [ 
     {"id": 1, "cell": {"id":"1", "desc":"Source 1", "num":"300"}}, 
     {"id": 2, "cell": {"id":"2", "desc":"Source 2", "num":"100"}} 
    ] 
} 

這是所有 不讀下一行

對不起,這是一些文本要經過警報:) 糟糕!你的問題不能提交的原因是:

您的文章沒有什麼太大的上下文解釋代碼段;請更清楚地解釋你的情況。

================ 感謝FO幫助 我的最終版本是從你的答案太

  jsonReader: { 
       repeatitems: false, 
      root: function (obj) { return obj; }, 
      page: function (obj) { return 1; }, 
      total: function (obj) { return 1; }, 
      records: function (obj) { return obj.length; }     
      } 

採取我已經改變了JSON數據

[ 
    {"id":"1", "desc":"Source 1", "num":"300"}, 
    {"id":"2", "desc":"Source 2", "num":"100"} 
] 

非常感謝!

回答

2

也許你的意思是數據的格式:

{ 
    "page": 1, 
    "total": 1, 
    "records": 2, 
    "rows": [ 
     {"id":"1", "desc":"Source 1", "num":"300"}, 
     {"id":"2", "desc":"Source 2", "num":"100"} 
    ] 
} 

沒有重複id和不必要的cell財產。要讀取數據,需要包括的jqGrid的​​選項(參見the documentation)。

0

感謝FO幫助 我的最終版本是從你的答案也採取

  jsonReader: { 
       repeatitems: false, 
      root: function (obj) { return obj; }, 
      page: function (obj) { return 1; }, 
      total: function (obj) { return 1; }, 
      records: function (obj) { return obj.length; }     
      } 

而且我已經改變了JSON數據

[ 
    {"id":"1", "desc":"Source 1", "num":"300"}, 
    {"id":"2", "desc":"Source 2", "num":"100"} 
] 

非常感謝!

+0

你應該更好地與添加此類的信息,而不是發佈新回答這個問題的文本。它簡化了其他訪問者對問題的閱讀。此外,您應該[接受](http://meta.stackexchange.com/a/5235/147495)解決問題最有幫助的答案。我建議你也閱讀[post](http://meta.stackexchange.com/a/81970/147495)關於語法突出顯示並比較你的問題和我的答案的代碼中的顏色。我建議你只爲所有的jqgrid問題添加「jquery」標籤。 – Oleg 2012-02-08 09:10:24

+0

你現在可以刪除答案。 – Oleg 2012-02-08 11:02:26