2012-11-23 92 views
0

有人可以幫我。我正在嘗試使用JQGrid動態呈現使用json的列和數據。列似乎正在出現,但沒有行數據。JQGrid顯示列但沒有行數據

下面是我從我的服務返回JSON:

{ 
    "total": 1, 
    "page": 1, 
    "records": 1, 
    "rows": [ 
     { 
      "id": 29291, 
      "cell": [ 
       "Jim", 
       "1", 
       "2" 
      ] 
     } 
    ], 
    "columns": [ 
     "Name", 
     "30/10/2012", 
     "23/10/2012" 
    ], 
    "columnmodel": [ 
     { 
      "name": "Name", 
      "index": "Name", 
      "align": "left", 
      "width": 25 
     }, 
     { 
      "name": "30/10/2012", 
      "index": "30/10/2012", 
      "align": "left", 
      "width": 25 
     }, 
     { 
      "name": "23/10/2012", 
      "index": "23/10/2012", 
      "align": "left", 
      "width": 25 
     } 
    ] 
} 

而且我現在用的就是JavaScript的:

$.ajax({ 
    type: "GET", 
    url: "ListData?ID=1", 
    datatype: "json", 
    success: function(result){ 
     $("#customgrid").jqGrid({ 
       datatype: "json", 
       colNames: result.columns, 
       colModel: result.columnmodel, 
       data: result.rows, 
       width: 800, 
       pager: "#customgridpager", 
       viewrecords: true, 
       sortable: true, 
       gridview: true, 
     }); 
    }, 
}); 

任何幫助將不勝感激。

回答

2

您只需對代碼進行一些小改動。您需要先更改$.ajax呼叫中的打字錯誤:將datatype更改爲dataType。那麼您需要更改datatype: "json"datatype: "jsonstring"data: result.rowsdatastr: result。完整的代碼,我會建議您低於:

$.ajax({ 
    type: "GET", 
    url: "NiallGray.json", 
    dataType: "json", 
    success: function (result) { 
     $("#customgrid").jqGrid({ 
      datatype: "jsonstring", 
      colNames: result.columns, 
      colModel: result.columnmodel, 
      datastr: result, 
      width: 800, 
      pager: "#customgridpager", 
      viewrecords: true, 
      sortable: true, 
      gridview: true, 
      rowNum: 10000, 
      height: "auto" 
     }); 
    } 
}); 

演示的修改後的版本是here

enter image description here

+0

傳奇!非常感謝。週末愉快。 –