Map JSON data in jqGrid

the code below creates a javascript object, converts it to JSON and tries to load it into jqGrid. I follow the wiki examples and I feel that I followed their example very precisely, but I still had no luck. Can anyone see what is missing 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'
        }
    })
+3
source share
1 answer

You do not need to convert the data to a JSON string. jqGrid will need to convert the data back. In this case, you should use datatype:'jsonstring'and datastr:gridData.

A better way would be to use only an array of elements:

var gridData = [
    { id: '1', col1: 'cell11', col2: 'cell12', col3: 'cell13' },
    { id: '2', col1: 'cell21', col2: 'cell22', col3: 'cell23' }
];
$('#jqgrid').jqGrid({
    data: gridData,
    datatype: 'local',
    ...
});
+5
source

Source: https://habr.com/ru/post/1794314/


All Articles