[英]Loop through data to create a dictionary with key/value pairs using JavaScript
[英]Using key/value pairs for jqGrid cell data
我有一個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加載數據時,它會自動將數據綁定到模型中的列?
看一看在jsonReader
的選項jqGrid的維基 ,特別是其repeatitems
財產。 從該頁面:
repeatitems元素告訴jqGrid,行中數據的信息是可重復的 - 即元素具有cell元素中描述的相同標記單元格。 將此選項設置為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"},
...
]}
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
元件。 此元素描述了數據的開始位置。 換句話說,這指向包含數據的數組。 如果我們設置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"]},
...
]
}
所以,如果你選擇鍵值方式; 單元格不應該在內容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
)數組數據在rows
關鍵字下直接顯示;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.