带有JSON输出的jqGrid [object 0bject]

时间:2011-02-16 11:13:20

标签: jquery jquery-plugins jqgrid

我遇到了jqGrid和JSON数据的问题。基本上,网格不会显示任何数据,而是将[object Object]输出到表的第一列。以下是我正在使用的数据的代码段。它是有效的JSON。

{
    "currpage": "1",
    "totalpages": "3",
    "totalrecords": "70",
    "rows": [
    {
        "id": "uid\u003dAndrewBryant",
        "cells": [
            {
                "HOURS_ENTITLED": "203",
                "HOURS_TAKEN": 0,
                "NAME": "uid\u003dAndrewBryant",
                "SICKNESS_TAKEN": 0,
                "TAKEN_TOIL": 0,
                "TOTAL_TOIL": 0,
                "YEAR_ENTILEMENT": "2011"
            }
        ]
    },

以下是我用来调用jqGrid功能的代码:

$(document).ready(function() {
    $("#statsTable").jqGrid({
        url: "http://cw-epuip-d01.tm-gnet.com:10040/wps/PA_Resource_Manager_1/JSONServlet",
        datatype: "json",
        mtype: "get",
        jsonReader : {
            root: "rows",
            cell:"cells",
            page: "currpage",
              total: "totalpages",
              records: "totalrecords",
               id: "id"
        },
        colNames: ['NAME', 'HOURS ENTITLED', 'HOURS TAKEN'  ],
        colModel: [
                  {name:'NAME', index:'NAME'},
                  {name:'HOURS_ENTITLED', index:'HOURS_ENTITLED'},
                  {name:'HOURS_TAKEN', index:'HOURS_TAKEN'}
            ]
    }
    );
});

我错过了什么?

1 个答案:

答案 0 :(得分:0)

首先,我不建议您使用“uid = AndrewBryant”作为id值。你应该明白它会跟随<tr id="uid=AndrewBryant">,这是非常糟糕的。有关正确ID的说明,请参阅here

接下来,您可以阅读当前生成的JSON文件,其中jsonReader(附加repeatitems:false)和每列的附加jsonmap属性进行了少量更改

jsonReader : {
    root: "rows",
    page: "currpage",
    total: "totalpages",
    records: "totalrecords",
    repeatitems: false
},
colModel: [
          {name:'NAME', index:'NAME',jsonmap:'cells.0.NAME'},
          {name:'HOURS_ENTITLED', index:'HOURS_ENTITLED',
           jsonmap:'cells.0.HOURS_ENTITLED'},
          {name:'HOURS_TAKEN', index:'HOURS_TAKEN',jsonmap:'cells.0.HOURS_TAKEN'}
]

更改后,将显示网格:请参阅here

尽管如此,我建议您最好在服务器代码中进行一些更改,并生成更多标准JSON,无需jsonmap即可读取。请参阅the jqGrid documentation