【问题标题】:How to show grid with root in jqGrid in JSON如何在 JSON 中的 jqGrid 中以 root 显示网格
【发布时间】:2012-06-30 14:13:57
【问题描述】:

我有一个返回 JSON 格式如下的服务器:

[{"Name": "Student1", "Email": "Email1", "CellPhone": null},
{{"Name": "Student2", "Email": "Email2", "CellPhone": null}]

HTML 文件有: <table id="list1></table> <div id="pager1"></div>

JS 文件是:

jQuery().ready(函数 () { jQuery("#list2").jqGrid({ url: 'Default.aspx?query=SELECT VALUE s FROM ModelContainer.StudentSet AS s WHERE s.Name = \'Student2\'', 数据类型:“json”, colNames:['姓名','电子邮件'], col型号:[ { 名称:'名称',索引:'名称',宽度:200 }, { 名称:'电子邮件',索引:'电子邮件',宽度:500 } ], 行数:10, 行列表:[10,20,30], 寻呼机:'#pager1', 排序名称:'名称', 观看记录:真实, 排序顺序:“desc”, 标题:“JSON 示例”, jsonReader:{ 重复项目:真实, 编号:“0”, 细胞: ””, //根: ””, 记录:函数(obj){ 返回 obj.length; } } }); jQuery("#list1").jqGrid('navGrid','#pager1',{edit:false,add:false,del:false}); });

由于我的 JSON 已经是所需数据的根,我不知道 root 列是什么。我试过不写任何关于 root 或root: "" 的东西,也没有输出。但firebug显示JSON接收正确。

我不知道这是否与根有关。有什么建议吗?

【问题讨论】:

    标签: jquery json jqgrid


    【解决方案1】:

    你可以使用

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

    但要使您提交的代码正常工作,您必须修复更多错误:

    • HTML 片段
    <table id="list1></table> <div id="pager1"></div>
    

    应该固定为

    <table id="list1"></table> <div id="pager1"></div>
    
    • jQuery("#list2").jqGrid({ 应固定为jQuery("#list1").jqGrid({
    • 必须修复 JSON 数据以消除语法错误,因为有一个不需要的 { 字符。正确的数据可以是
    [{"Name": "Student1", "Email": "Email1", "CellPhone": null},
     {"Name": "Student2", "Email": "Email2", "CellPhone": null}]
    

    The fixed demo 有效。

    【讨论】:

    • @Bruno:谢谢!我同意你的看法。当时是jqGrid的4.4.0版本。稍后我建议自动检测 JSON 格式。该功能从 4.4.5 版本开始存在(并且仅在 4.5.0 版本中修复了一个重要错误)。所以使用repeatitems: true 的公认解决方案是行不通的。此外,问题是关于 array of items 并且接受的答案建议将格式更改为 rows : "details" 而不是仅使用 root: function (obj) {return obj;} 这将是正确的方法。错误接受的解决方案可能会使其他读者感到困惑,但我无法更改它。
    【解决方案2】:

    据我所知,我们需要在 jsonReader 中设置 JSON 对象名称如下:

    jsonReader: {
                repeatitems: true,
                id: "0",
                cell: "",
                rows : "details",
                page : "pageno"
                }
    

    JSON 格式:

     {"pageno":"1", "details" : [{"Name": "Student1", "Email": "Email1", "CellPhone": null},   
        {{"Name": "Student2", "Email": "Email2", "CellPhone": null}] 
    

    jsonreader 选项:

    http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data#json_data

    【讨论】:

    • id in jsonReader 是指行号吗?如果是这样,如果我写id: "0"会发生什么?
    • No 实际上 id 只不过是唯一的行 id。编辑我的答案请检查。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多