【问题标题】:jsonReader and jqgrid not populating json onto gridjsonReader 和 jqgrid 没有将 json 填充到网格上
【发布时间】:2014-07-29 19:29:11
【问题描述】:

我有下面的 json,我想要显示在我的 jqgrid 上。我有以下 jsonReader

jsonReader : {
            repeatitems: false,
            root: "abc",
            page: function (obj) { return 1; },
            total: function (obj) { return 1; },
            records: function (obj) { return obj.length; }
        },

列模型:

colModel:[
            {name:'num'},
            {name:'seq'},
            {name:'status'},
            {name:'transTime'},
            {name:'sd'},    
            {name:'total'},
            {name:'xys'}
        ],

Json:

{
    "xys": 3,
    "abc": [
        {
            "time": null,
            "num": "1234",
            "seq": 2,
            "status": "X",
            "transTime": null
        },
        {
            "time": null,
            "num": "4567",
            "seq": 1,
            "status": "Y",
            "transTime": null
        }
    ],
    "sd": "7895",
    "total": 5
}

只有 num、seq 和 status 被填充了数据,而不是 transTime、sd、total 和 xys

有什么想法和建议吗?

【问题讨论】:

  • sdtotalxys 不是 abc 数组的属性。您想如何在网格中显示项目?在相应列的每一行中重复? transTime 为空,不会显示为“null”字符串,只是显示为空字符串“”。
  • 感谢奥列格!我想显示该 JSON 中返回的所有数据。我想在网格上显示 xys、sd、total 和 abc 数组。我了解 time 和 transTime 为空,因此它们不会显示。我希望能够显示 xys、sd 和 total 以及 abc 数组
  • 最简单的方法是将sdtotalxys 属性作为abc 项目的属性。您还可以将"transTime": null 更改为"transTime": "null"。您可以在服务器端执行此操作吗?如果没有,则可以在beforeProcessing 回调中在客户端更改输入数据。如果您遇到实施问题,我可以向您展示如何做到这一点。
  • 感谢奥列格!你能告诉我怎么做吗?从服务器返回的 JSON 是固定的,我无法进行更改。

标签: jquery ajax json jqgrid jsonreader


【解决方案1】:

如果您必须读取 JSON 数据并且无法更改服务器端的数据,您可以对从服务器返回的数据进行小的转换在客户端 em> 在beforeProcessing 回调中。 The demo 执行此操作并显示以下结果

它使用以下代码

loadonce: true,
jsonReader: { root: "abc" },
beforeProcessing: function (data) {
    var root = data.abc, i, item, cItems = root.length;
    for (i = 0; i < cItems; i++) {
        item = root[i];
        item.sd = data.sd;
        item.id = $.jgrid.randId();
        item.total = data.total;
        item.xys = data.xys;
        if (item.transTime === null) {
            item.transTime = "null";
        }
    }
}

【讨论】:

  • Oleg,我收到以下错误:Uncaught TypeError: Cannot read property 'id' of undefined jquery.jqGrid.min.js:66
  • @user3420947:您使用哪个版本的 jqGrid? 我在代码中包含了 item.id = $.jgrid.randId(); 行,但这不是必需的。如果您遇到错误并在 jqGrid 的 SRC 版本中发布行号,则应始终使用 jquery.jqGrid.src.js 而不是 jquery.jqGrid.min.js
  • 我使用 jquery.jqGrid.min.js 版本 4.2.0。我更改为 jquery.jqGrid.src.js 仍然得到 Uncaught TypeError: Cannot read property 'id' of undefined jquery.jqGrid.src.js:1281
  • @user3420947:我想你在beforeProcessing 中没有使用item.id = $.jgrid.randId(); 行。我不建议你使用这么旧版本的 jqGrid。它于 2011 年发布。无论如何the modified demo 也适用于 jqGrid 4.2.0。如果您的数据仍然存在相同的错误,您可以在jsonReader 中包含cell: false 属性。
猜你喜欢
  • 1970-01-01
  • 2021-05-31
  • 1970-01-01
  • 1970-01-01
  • 2011-09-21
  • 1970-01-01
  • 1970-01-01
  • 2020-09-20
  • 2012-08-02
相关资源
最近更新 更多