Javascript 对jqGrid单元格数据使用键/值对
我将jqGrid定义为:Javascript 对jqGrid单元格数据使用键/值对,javascript,jqgrid,Javascript,Jqgrid,我将jqGrid定义为: $("#tableFeedbackReports").jqGrid({ url: '/FeedbackReports/GetFeedbackReport', datatype: 'json', colNames: ['ColA', 'ColB', 'ColC', 'ColD'], colModel: [{ name: 'ColA', index: 'ColA', width:
$("#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
选项,特别是它的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"},
...
]
}
在
元素。此元素描述数据的起始位置。换句话说,这指向包含数据的数组。如果我们开始
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”
,“单元格”
关键字不存在,关联(键值
)数组数据直接位于行
关键字下 多谢各位。这件事让我绞尽脑汁好几个小时了!
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"}]