下面的代码创建一个javascript对象,将其转换为JSON,并尝试将其加载到jqGrid中。 我一直在关注维基的例子,我觉得我已经非常准确地追随他们,但仍然没有运气。 任何人都可以看到这里缺少的链接?
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' } })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 。
最好的方法是只使用项目数组:
var gridData = [ { id: '1', col1: 'cell11', col2: 'cell12', col3: 'cell13' }, { id: '2', col1: 'cell21', col2: 'cell22', col3: 'cell23' } ]; $('#jqgrid').jqGrid({ data: gridData, datatype: 'local', ... });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', ... });更多推荐
发布评论