【问题标题】:Json data doesnot get binded to jqgrid in spring,struts2Json数据在spring,struts2中没有绑定到jqgrid
【发布时间】:2014-07-16 06:28:53
【问题描述】:

我在 JQGrid 中显示来自 Json 响应的数据时遇到问题。从 Action 收到的 JSON 响应是

{
"deviceInfoList": [
    {

        "androidVersion": "4.3",
        "deviceImageUrl": "//image",
        "deviceName": "generic",
        "deviceNetwork": {
            "deviceId": "000000000000000"
            "serviceState": "STATE_OUT_OF_SERVICE",
            "signalStrength": 0
        },
        "deviceSettingsInfo": {
            "chargingStatus": 50,
            "currentLocation": "",
            "macId": "DEFAULT",
            "rowStatus": true,

        },
        "deviceType": "Android",
        "emailId": " "
        "phoneNumber": "15555215554",

    }

]

}

这些值可以在控制台中看到,但 JQGrid 仍然为空。我正在使用 Spring、Struts2 和 Hibernate 集成。请帮助。谢谢。

网格代码:

$("#grid").jqGrid(
        {
            url :'loadGrid',
            datatype : "json",
            mtype: 'GET',
            height : 230,
            autowidth : true,
            multiselect : true,
            colNames : [ 'IMEI', 'MAC ID', 'Name', 'Base Location',
                    'Current Location', 'OS and Version', 'Device Type',
                    'Status', 'Last Contacted' ],
            colModel : [ {
                name : 'imeiNo1',
                index : 'imeiNo1',

            }, {
                name : 'macId',
                index : 'macId'
            }, {
                name : 'deviceName',
                index : 'deviceName'
            }, {
                name : 'ipAddress',
                index : 'ipAddress'
            }, {
                name : 'currentLocation',
                index : 'currentLocation'
            }, {
                name : 'androidVersion',
                index : 'androidVersion'
            }, {
                name : 'deviceType',
                index : 'deviceType'
            }, {
                name : 'chargingStatus',
                index : 'chargingStatus'
            }, {
                name : 'deviceAddedDate',
                index : 'deviceAddedDate'
            } ],
            sortname : 'id',
            pager : '#pager',
            caption : "Device Details",
            hidegrid : false,
            pgbuttons : false, // disable page control like next, back
            pgtext : null, // disable pager text like 'Page 0 of 10'
            viewrecords : false,

             jsonReader : {
                 root: "rows",
                 page: "page",
                 total: "total",
                 records: "records",
                 repeatitems: true,
                 cell: "cell",
                 id: "id",
                 userdata: "userdata",
                 subgrid: {root:"rows", 
                    repeatitems: true, 
                   cell:"cell"
                 }
               }
        });

var names = [ "imeiNo1", "macId", "deviceName", "ipAddress", "currentLocation",
        "androidVersion", "deviceType", "chargingStatus", "deviceAddedDate" ];
var mydata = [];

for ( var i = 0; i < data.length; i++) {
    mydata[i] = {};
    for ( var j = 0; j < data[i].length; j++) {
        mydata[i][names[j]] = data[i][j];
    }
}

for ( var i = 0; i <= mydata.length; i++) {
    $("#grid").jqGrid('addRowData', i + 1, mydata[i]);
}

$("#grid").jqGrid('navGrid', '#pager', {
    edit : false,
    view : false,
    add : false,
    del : false,
    search : false,
    beforeRefresh : function() {
        // alert('In beforeRefresh');
        grid.jqGrid('setGridParam', {
            datatype : 'json'
        }).trigger('reloadGrid');
    }
});

【问题讨论】:

  • 请将您的代码发布到您创建网格的位置

标签: json spring hibernate struts2 jqgrid


【解决方案1】:

根据 jqGrid 文档here

第一个元素是一个

元素。这个元素描述了我们的数据从哪里开始。换句话说, this 指向包含数据的数组...

这意味着root: "deviceInfoList" 应该在您的脚本中。

【讨论】:

  • 谢谢。现在 json 值已绑定但未完全绑定。macId、ipAddress、充电状态仍未绑定。您可以查看 json 并提出建议吗?
  • .macId, ipAddress 似乎是 json 中的嵌套属性。 colModel 应该有这样的东西{name : 'deviceSettingsInfo.macId', index : 'deviceSettingsInfo.macId'}read this
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-12
  • 2019-09-19
  • 2011-11-18
  • 2017-12-20
  • 1970-01-01
相关资源
最近更新 更多