map JSON data to jqGrid
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(grid开发者_运维问答Data);
$('#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'
}
})
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',
...
});
精彩评论