2010-02-09 39 views
0

我正在嘗試使用jqGrid獲取顯示的網格。一切似乎都很好。表格正在渲染,但所有的單元格都是空的。所有其他信息都在表格上(頁碼,總頁數,行數)。當試圖改變頁面時,正在檢索json數據沒有任何問題。jqGrid - 表未由json請求的數據填充

這裏是我的代碼剪斷:發送

<script type="text/javascript"> 
$(document).ready(function() { 
    $("#list2").jqGrid({ 
     url:'/ajax/list/facture', 
     datatype: "json",   
     colModel:[ 
     {label:'N° d\'article', name: 'code', width:90}, 
     {label:'Article', name: 'article', width:100}, 
     {label:'Entrepôt', name: 'entrepot', width:80, align:"right"}, 
     {label:'Limite', name: 'limite', width:80, align:"right"}, 
     {label:'À commander', name: 'qte_a_commander', width:80,align:"right"}, 
     {label:'Déjà commander', name: 'qte_deja_commander', width:150}, 
     {label:'Coût', name: 'cout', width:150}, 
     {label:'Prix', name: 'prix', width:150}, 
     {label:'Coût total', name: 'cout_total', width:150} 
     ], 
     rowNum:100, 
     scoll: true, 
     //rowList:[10,20,30], 
     pager: '#pager2', 
     //sortname: 'code', 
     viewrecords: true, 
     sortorder: "desc", 
     jsonReader: { 
     repeatitems : false, 
     id: "0" 
     }, 
     //sortorder: "desc", 
     caption:"Inventaire", 

     width: 1200, 
     height: 200 


    }); 

    $("#list2").jqGrid('navGrid','#pager2',{edit:false,add:false,del:false}); 


}); 


</script> 

<table id="list2"></table> 
<div id="pager2"></div> 

我的JSON數據:

{ 
    "page":"1", 
    "total":33, 
    "records":"100", 
    "rows":[ 
    {"id":1,"cell":{"code":"0064NB","article":"Livre","entrepot":"4","limite":"3","qte_a_commander":"3","qte_deja_commander":"0","cout":"3.40","prix":"30.99","cout_total":"13.60"}}, 
    {"id":2,"cell":{"code":"0072NB","article":"Livre et corrig\u00e9","entrepot":"5","limite":"3","qte_a_commander":"3","qte_deja_commander":"0","cout":"3.40","prix":"30.99","cout_total":"17.00"}} 
    /*[... got over 100 fields ...]*/ 
    ]} 

回答

1

好,能得到它的工作使用索引而不是名稱

colModel:[ 
    {label:'Code', index: 'code', width:90}, 
    {label:'Article', index: 'article', width:100}, 
    {label:'Entrepôt', index: 'entrepot', width:80, align:"right"}, 
    {label:'Limite', index: 'limite', width:80, align:"right"}, 
    {label:'À commander', index: 'qte_a_commander', width:80,align:"right"}, 
    {label:'Déjà commander', index: 'qte_deja_commander', width:150}, 
    {label:'Coût', index: 'cout', width:150}, 
    {label:'Prix', index: 'prix', width:150}, 
    {label:'Coût total', index: 'cout_total', width:150} 
    ], 

,並通過未命名的json數據:

{ 
    "page":"1", 
    "total":33, 
    "records":"100", 
    "rows": 
    [ 
     {"id":1,"cell":["0064NB","Livre","4","3","3","0","3.40","30.99","13.60"]}, 
     {"id":2,"cell":["0072NB","Livre corrig\u00e9","5","3","3","0","3.40","30.99","17.00"]} 
    ] 
} 
+0

我不知道你的解決方案爲什麼起作用,因爲根據jqGrid文檔(http://www.trirand.com/jqgridwiki/doku.php?id=wiki:colmodel_options),「name」屬性是必需的。 「 」在列的網格中設置唯一名稱,該屬性是必需的。「 – 2010-03-18 15:07:38

1

我不認爲這是正確的格式。您定義的JSONReader預計這樣

{ 
    "page":"1", 
    "total":33, 
    "records":"100", 
    "rows":[ 
    {"id":1,"code":"0064NB","article":"Livre","entrepot":"4","limite":"3","qte_a_commander":"3","qte_deja_commander":"0","cout":"3.40","prix":"30.99","cout_total":"13.60"}, 
    {"id":2,"code":"0072NB","article":"Livre et corrig\u00e9","entrepot":"5","limite":"3","qte_a_commander":"3","qte_deja_commander":"0","cout":"3.40","prix":"30.99","cout_total":"17.00"} 
    /*[... got over 100 fields ...]*/ 
    ]} 

JSON數據閱讀retrieving data in the jqGrid wiki的章節。實際上,我發現更改服務器端JSON輸出更容易,而不是爲jQGrid定義自定義閱讀器。

+0

我的錯。我做了一些json數據的格式化,並忘記刪除它。 其實,我的PHP腳本只是打印一個json_encode()調用。 – Caissy 2010-02-09 20:11:41

+0

是的,那麼你的腳本必須按照上面提到的方式生成編碼數組。還是不行? – Daff 2010-02-09 21:13:16