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

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

Leave a Comment