如何将JSON数据加载到Jqgrid?

Use*_*ser 3 javascript jquery json jqgrid

我使用以下方式将数据加载到jqgrid中.我无法将json数据加载到jqgrid中.我将json解析为数组,如mydata = json.parse(jsondata).然后我绑定此数组(mydata)使用数据类型进入jqgrid :"local" .my问题是如何将json数据绑定到jqgrid?

         $("#datagrid").jqGrid({

                    datatype: "local", 
                    data: mydata,
                    colNames:['companyid','company', 'price', 'Change','perchange','LastUpdated'],
                    colModel:[
                        {name:'companyid',index:'companyid', width:100,editable:true,editoptions:{size:10}},
                        {name:'company',index:'company', width:100,editable:true},
                        {name:'price',index:'price', width:100,editable:true,editoptions:{size:10}},
                        {name:'Change',index:'Change', width:100,editable:true,editoptions:{size:25}},
                        {name:'perchange',index:'perchange', width:100, align:"right",editable:true,editoptions:{size:10}},
                        {name:'LastUpdated',index:'LastUpdated', width:200, align:"right",editable:true,editoptions:{size:10}}
                    ],
                    rowNum:10,
                    rowList:[3,6],
                    loadonce: true,
                    pager: '#navGrid',
                    sortname: 'companyid',
                    sortorder: "asc", 
                    height: 210,
                    width:600,
                    onSelectRow: function(id)
                                 {
                                    getID = jQuery("#datagrid").jqGrid('getCell', id, 'companyid')
                                 },
                    viewrecords: true,
                    caption:"JQ GRID"
                }); 
Run Code Online (Sandbox Code Playgroud)

JSON格式:

    [
        {
            "company": "test",
            "price": 98,
            "Change": 8,
            "perchange": 8,
            "LastUpdated": "2",
            "companyid": 2
        },
        {
            "company": "test123",
            "price": 1,
            "Change": 1,
            "perchange": 1,
            "LastUpdated": "1",
            "companyid": 3
        },
        {
            "company": "abc",
            "price": 1234,
            "Change": 123,
            "perchange": 1,
            "LastUpdated": "1",
            "companyid": 1
        }
    ]
Run Code Online (Sandbox Code Playgroud)

Ole*_*leg 7

首先,您需要id在输入数据中定义行.id每行(<tr>)的属性将在相应的值中设置.因为您已经companyid可以扮演角色,所以添加key: true"companyid"列的属性就足够了colModel.

直接从服务器加载日期的问题(包括从文件中加载)可以通过添加jsonReader描述输入数据格式来解决.因为您使用loadonce: truetotal,输入数据recordspage属性将被忽略,您可以使用jsonReader以下简单形式:

jsonReader: {repeatitems: false, root: function (obj) { return obj; }}
Run Code Online (Sandbox Code Playgroud)

相应的演示在这里.

如果您需要从您发布的数据数组中加载数据,您的代码应直接工作(请参阅另一个演示).我想在解析JSON数据时有一些其他问题,但是你没有发布相应的代码.

关于id和的建议key: true仍在进行中.您可以使用localReader: {id: "companyid"}第二种情况及同一物业id: "companyid"jsonReader交替.我个人更喜欢使用,key: true因为代码易于阅读,并且独立于所使用的阅读器.