How can I use nested Json to populate Kendo UI grid?

后端 未结 2 683
轮回少年
轮回少年 2020-12-03 12:52

How can I populate Kendo UI grid with nested JSON.

I mean my JSON is like

var myJson:
    [{\"oneType\":[
        {\"id\":1,\"name\":\"John Doe\"},
          


        
2条回答
  •  旧巷少年郎
    2020-12-03 13:38

    For complex JSON structures, you might use 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");
    

    If you slightly change your JSON to:

    {
        "oneType"   : [
            {"id": 1, "name": "John Doe"},
            {"id": 2, "name": "Don Joeh"}
        ],
        "othertype" : "working",
        "otherstuff": "xyz"
    }
    

    then you can use:

    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");
    

提交回复
热议问题