如何使用嵌套的Json填充Kendo UI网格?

Sau*_*abh 6 javascript json kendo-ui kendo-grid

如何使用嵌套的JSON填充Kendo UI网格.

我的意思是我的JSON就像

var myJson:
    [{"oneType":[
        {"id":1,"name":"John Doe"},
        {"id":2,"name":"Don Joeh"}
    ]},
    {"othertype":"working"},
    {"otherstuff":"xyz"}]
}];
Run Code Online (Sandbox Code Playgroud)

我希望Kendo UI Grid的列为Id,Name,OtherType和OtherStuff.

提前致谢.!

Ona*_*Bai 10

对于复杂的JSON结构,您可以使用 schema.parse

var grid = $("#grid").kendoGrid({
    dataSource : {
        data    : [
            {
                "oneType": [
                    {"id": 1, "name": "John Doe"},
                    {"id": 2, "name": "Don Joeh"}
                ]
            },
            {"othertype": "working"},
            {"otherstuff": "xyz"}
        ],
        pageSize: 10,
        schema  : {
            parse : function(d) {
                for (var i = 0; i < d.length; i++) {
                    if (d[i].oneType) {
                        return d[i].oneType;
                    }
                }
                return [];
            }
        }
    }
}).data("kendoGrid");
Run Code Online (Sandbox Code Playgroud)

如果您稍微将JSON更改为:

{
    "oneType"   : [
        {"id": 1, "name": "John Doe"},
        {"id": 2, "name": "Don Joeh"}
    ],
    "othertype" : "working",
    "otherstuff": "xyz"
}
Run Code Online (Sandbox Code Playgroud)

然后你可以使用:

var grid = $("#grid").kendoGrid({
    dataSource: {
        data    : {
            "oneType"   : [
                {"id": 1, "name": "John Doe"},
                {"id": 2, "name": "Don Joeh"}
            ],
            "othertype" : "working",
            "otherstuff": "xyz"
        },
        pageSize: 10,
        schema  : {
            data: "oneType"
        }
    }
}).data("kendoGrid");
Run Code Online (Sandbox Code Playgroud)