2010-01-14 41 views
4

鍵/值對我有一個jqGrid的定義是這樣的:使用的jqGrid的單元格數據

$("#tableFeedbackReports").jqGrid({ 
      url: '/FeedbackReports/GetFeedbackReport', 
      datatype: 'json', 
      colNames: ['ColA', 'ColB', 'ColC', 'ColD'], 
      colModel: [{ name: 'ColA', index: 'ColA', width: 60 }, 
         { name: 'ColB', index: 'ColB', width: 60 }, 
         { name: 'ColC', index: 'ColC', width: 60 }, 
         { name: 'ColD', index: 'ColD', width: 60 }, 
/* ... and so on */ 

現在,當Ajax調用返回時,它必須返回的東西將進入各行的一個數組。

['value', 'value', 'value'] 

是否有可能讓jqGrid接受行數據的鍵/值對?

[{ 'ColA' : 'value', 'ColB' : 'value', 'ColC' : 'value', 'ColD' : 'value'}] 

所以當jqGrid的加載數據,它會自動將數據綁定到模型中的列?

回答

4

看看jqGrid Wiki上的jsonReader選項,特別是它的repeatitems屬性。從該網頁:

的repeatitems元素告訴的jqGrid,對於 行中的數據的信息是可重複的 - 即,元件具有在細胞 元件所描述的相同的標記細胞。將此選項設置爲false將指示jqGrid按名稱搜索 中的json數據元素。這是colModel中的名稱,或者是在colModel中使用 jsonmap選項描述的名稱。

他們的例子是:

jQuery("#gridid").jqGrid({ 
... 
    jsonReader : { 
     root:"invdata", 
     page: "currpage", 
     total: "totalpages", 
     records: "totalrecords", 
     repeatitems: false, 
     id: "0" 
    }, 
... 
}); 

將處理在下面的格式的數據,與鍵/值對:

{ 
    totalpages: "xxx", 
    currpage: "yyy", 
    totalrecords: "zzz", 
    invdata : [ 
    {invid:"1",invdate:"cell11", amount:"cell12", tax:"cell13", total:"1234", note:"somenote"}, 
    {invid:"2",invdate:"cell21", amount:"cell22", tax:"cell23", total:"2345", note:"some note"}, 
    ... 

] }

+0

完美。謝謝! – 1kevgriff

+2

非常感謝。一直在我的大腦上花了幾個小時! –

0
Tag Description 
total total pages for the query 
page current page of the query 
records total number of records for the query 
rows an array that contains the actual data 
    id the unique id of the row 
cell an array that contains the data for a row 

http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data#json_data

root 

element。這個元素描述了我們數據的開始位置換句話說,這指向包含數據的數組。如果我們設置了 jQuery(「#gridid」)。jqGrid({... jsonReader:{root:「invdata」}, ... }); 那麼返回的字符串應該是

{ 
    "total": "xxx", 
    "page": "yyy", 
    "records": "zzz", 
    "invdata" : [ 
    {"id" :"1", "cell" :["cell11", "cell12", "cell13"]}, 
    {"id" :"2", "cell":["cell21", "cell22", "cell23"]}, 
    ... 
    ] 
} 

所以如果u選擇鍵值的方式;單元格不應該在內容json字符串中,但行應該;

jQuery("#gridid").jqGrid({ 
... 
    jsonReader : { 
     repeatitems: false, 
    }, 
... 
}); 

所得數據應該是:

{"page":"1","total":1,"records":"1", 
"rows": [ 
    {"invid" : "1","invdate":"cell11", "amount" :"cell12", "tax" :"cell13", "total" :"1234", "note" :"somenote"}, 
    {"invid" : "2","invdate":"cell21", "amount" :"cell22", "tax" :"cell23", "total" :"2345", "note" :"some note"}] 

看到"id":"1""cell"關鍵字是出來,並且關聯(key value)陣列的數據derectly下rows關鍵字;

相關問題