【问题标题】:How can I get this JSON to jqGrid?我怎样才能得到这个 JSON 到 jqGrid?
【发布时间】:2013-10-31 07:09:56
【问题描述】:

我无法从无法访问的服务器读取此类 JSON,因此无法操作 JSON:

{
    "API_ICDFactory": {
        "API_getDataSample": {
            "key_0": {
                "dateTransaction": "1379454296",
                "dateBilling": "1387320296",
                "units": "181",
                "priceUnit": "25.12",
                "amount": "4546.72",
                "company": "Juan Vivas Taller",
                "productRef": "CAR",
                "productDesc": "Detergente especial antiestático"
            },
            "key_1": {
                "dateTransaction": "1377421074",
                "dateBilling": "1385373474",
                "units": "137",
                "priceUnit": "8.99",
                "amount": "1231.63",
                "company": "Autos Caceres 2000",
                "productRef": "BRICAR",
                "productDesc": "Cera hidrofugante para túneles de lavado"
            },
            "status": "success"
        }
    }
}

我不知道如何从这个 Json 中获取元素。如果有人可以帮助我做到这一点。

我正在尝试操作 jqGrid 文档中的这段代码,但我无法得到任何结果。

jQuery(document).ready(function(){ 
    jQuery("#grid").jqGrid({
        url: "url...",
        datatype: "json",
        mtype: "GET",
        colNames: ['Fecha Trans','Fecha Pago', 'Cliente'],
        colModel: [
            {name:'dateTransaction',index:'dateTransaction', width:100,editable:true},
            {name:'dateBilling',index:'dateBilling', width:100,editable:true},
            {name:'company',index:'company', width:100,editable:true}
        ],
        jsonReader: {
            repeatitems:false
        },
        rowNum:10,
        rowList:[10,20,30],
        pager: jQuery('#pager'),
        sortname: 'name',
        viewrecords: true,
        sortorder: 'asc',
        caption:'Title',
        editurl:'url...'
   }).navGrid('#pager');
});

如果有人能告诉我这个 JSON 的正确语法,我可以做剩下的。

谢谢!

【问题讨论】:

    标签: javascript jquery json jqgrid


    【解决方案1】:

    您必须将从服务器返回的 JSON 数据转换为 jqGrid 可以读取的项目数组。此外,我建议您使用 loadonce: true 选项,以便您可以在 jqGrid 中使用本地分页、排序和过滤/搜索。

    您可以通过将root 部分jsonReader 定义为函数来转换输入数据。或者,您可以使用 beforeProcessing 回调来更改服务器响应。例如你可以使用下面的jsonReader

    jsonReader: {
        root: function (obj) {
            var input = obj.API_ICDFactory.API_getDataSample, p, res = [], item;
            for (p in input) {
                item = input[p];
                if (input.hasOwnProperty(p) && typeof item === "object") {
                    item.id = p;
                    res.push(item);
                }
            }
            return res;
        },
        repeatitems: false
    }
    

    The demo 证明我的建议。它显示

    我使用的完整代码可以在下面找到:

    $(function () {
        "use strict";
        var intTemplate = { width: 100, formatter: "integer", sorttype: "integer", align: "right" };
        $("#grid").jqGrid({
            url: "victorgb6.json",
            datatype: "json",
            colNames: ["Fecha Trans", "Fecha Pago", "Cliente"],
            colModel: [
                { name: "dateTransaction", template: intTemplate },
                { name: "dateBilling", template: intTemplate },
                { name: "company" }
            ],
            cmTemplate: {width: 250, editable: true},
            gridview: true,
            height: "auto",
            autoencode: true,
            loadonce: true,
            jsonReader: {
                root: function (obj) {
                    var input = obj.API_ICDFactory.API_getDataSample, p, res = [], item;
                    for (p in input) {
                        item = input[p];
                        if (input.hasOwnProperty(p) && typeof item === "object") {
                            item.id = p;
                            res.push(item);
                        }
                    }
                    return res;
                },
                repeatitems: false
            },
            rowNum: 10,
            rowList: [10, 20, 30],
            pager: "#pager",
            viewrecords: true,
            caption: "Title",
            editurl: "myEditUrl"
        }).jqGrid("navGrid", "#pager");
    });
    

    【讨论】:

    • 您好,我已经使用了您的代码,它适用于本地文件,但是当我尝试从服务器获取 JSON 时,什么也没做。好吧,首先我收到一个错误:Access-Control-Allow-Origin 不允许 Origin localhost,但我想我设法使用我在这篇文章中阅读的代理文件解决了它link我不明白现在出错但没有数据。我必须获取 JSON 的服务器的 URL 是:panel.virtualcenter360.es/home/api/…
    猜你喜欢
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 2018-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多