【问题标题】:jqGrid initialized empty when using JSON data使用 JSON 数据时 jqGrid 初始化为空
【发布时间】:2011-06-23 11:16:04
【问题描述】:

我一直在各种论坛上闲逛,并阅读了几个与此问题相关的 stackoverflow 问题,但我终其一生都无法弄清楚出了什么问题。

我尝试使用以下代码生成一个 jqGrid treeGrid:

      jQuery("#structureBuilderTable").jqGrid({
        url: 'tree.json',
        datatype:'json',
        mtype:'GET',
        colNames: ["ID", "Description", "Total"],
        colModel: [
        {name:'id', index:'id', width: 1, hidden: true, key: true},
        {name:'desc', index:'desc', hidden: false, sortable: true},
        {name:'num', index:'num', hidden: false, sortable: true}
        ],
        treeGridModel:'adjacency',
        height:'auto',
        width:'500',
        pager:"#ptreegrid",
        treeGrid: true,
        ExpandColumn:'desc',
        ExpandColClick: true,
        caption:"TreeGrid Test"
      });

这是我的 .json 文件(作为示例):

{
    "total": "1",
    "page": "1",
    "records": "2",
    "rows": [
           {"id": "1", "cell": ["1", "Super Item", "300", "0", "null", "false", "false"]},
           {"id": "2", "cell": ["2", "Item 1", "100", "1", "1", "false", "false"]},
           {"id": "3", "cell": ["3", "Sub Item 1", "50", "2", "2", "true", "true"]},
           {"id": "4", "cell": ["4", "Sub Item 2", "25", "2", "2", "false", "false"]},
           {"id": "5", "cell": ["5", "Sub-sub Item 1", "25", "3", "4", "true", "true"]},
           {"id": "6", "cell": ["6", "Sub Item 3", "25", "2", "2", "true", "true"]},
           {"id": "7", "cell": ["7", "Item 2", "200", "1", "1", "false", "false"]},
           {"id": "8", "cell": ["8", "Sub Item 1", "100", "2", "7", "false", "false"]},
           {"id": "9", "cell": ["9", "Sub-sub Item 1", "50", "3", "8", "true", "true"]},
           {"id": "10", "cell": ["10", "Sub-sub Item 2", "50", "3", "8", "true", "true"]},
           {"id": "11", "cell": ["11", "Sub Item 2", "100", "2", "7", "true", "true"]}
    ]
}

(这几乎是我在网上找到的指南的直接副本)。

现在,网格已生成,但它不包含任何数据。 javascript 文件与“tree.json”位于同一目录中,但不知何故似乎找不到它。我将以下内容用于调试目的:

loadError: function(xhr, status, error) {alert(status +error)}

这是我收到的警报:

找不到错误

任何帮助都会非常感激。

【问题讨论】:

  • 您是否尝试使用 Firebug 或其他工具进行调试以查看 JSON 是否实际加载正确?好像是找不到JSON。
  • 是的,这就是问题所在。我似乎没有找到 .json 文件(可能我对这部分不够清楚)。
  • 这很奇怪。我已经用你的选项和你的 JSON 做了一个基本的测试,一切似乎都很好。我不得不注释掉 pager 属性,但我怀疑这是你问题的原因。可以在这里找到一个工作示例:minvis.nl/jqGrid/tree.html 也许它对您有用。

标签: javascript jquery json jqgrid treegrid


【解决方案1】:

我希望 the demo 基于您的 JSON 数据和 jqGrid 将帮助您找到代码中的错误。可能您只是忘记将创建网格的代码放在jQuery(function(){/**/}); 内。

只有一个提示:如果您希望某些树节点像我的演示一样显示为展开状态,您不仅需要在最后一列(“展开”隐藏列)中设置“真”值,还需要添加树网格的隐藏“加载”列的“真”值。请参阅herehere 了解更多详情。

【讨论】:

  • 非常感谢你们的帮助! :-) 原来这是一个关于项目配置的外部错误导致错误。我会好好利用你的两个演示!
猜你喜欢
  • 1970-01-01
  • 2013-10-31
  • 2014-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-01
相关资源
最近更新 更多