将JSON数据映射到jqGrid [英] map JSON data to jqGrid
问题描述
下面的代码创建一个javascript对象,将其转换为JSON,然后尝试将其加载到jqGrid中.我一直在跟踪Wiki示例,但我觉得我已经非常精确地遵循了它们的指导,但是仍然没有运气.谁能看到这里缺少的链接?
the below code creates a javascript object, converts it to JSON, and attempts to load it into a jqGrid. I have been following the wiki examples, and I feel I have followed their lead very precisely, but still am having no luck. Can anyone see what the missing link is here?
jQuery(document).ready(function () {
var gridData = {
total: 2,
page: '1',
records: '12',
rows: [
{ id: '1', col1: 'cell11', col2: 'cell12', col3: 'cell13' },
{ id: '2', col1: 'cell21', col2: 'cell22', col3: 'cell23' }
]
};
gridData = $.toJSON(gridData);
$('#jqgrid').jqGrid({
data: gridData,
datatype: 'json',
colNames: ['Col1', 'Col2', 'Col3'],
colModel: [
{ name: 'col1' },
{ name: 'col2' },
{ name: 'col3' }
],
jsonReader: {
root: 'rows',
total: 'total',
page: 'page',
records: 'records',
repeatitems: false,
id: '0'
}
})
推荐答案
您不需要将数据转换为JSON字符串. jqGrid将不得不将数据转换回去.在这种情况下,您应该使用datatype:'jsonstring'
和datastr:gridData
.
You don't need convert the data to JSON string. jqGrid will have to convert the data back. In the case you should use datatype:'jsonstring'
and datastr:gridData
.
最好的方法是只使用项目数组:
The best way would be to use just array of item:
var gridData = [
{ id: '1', col1: 'cell11', col2: 'cell12', col3: 'cell13' },
{ id: '2', col1: 'cell21', col2: 'cell22', col3: 'cell23' }
];
$('#jqgrid').jqGrid({
data: gridData,
datatype: 'local',
...
});
这篇关于将JSON数据映射到jqGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!