Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

How can I populate Kendo UI grid with nested JSON.

I mean my JSON is like

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

and I want Kendo UI Grid with columns as Id, Name, OtherType and OtherStuff.

Thanks in advance.!

like image 539
Saurabh Avatar asked Jun 21 '13 08:06

Saurabh


1 Answers

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");
like image 171
OnaBai Avatar answered Sep 22 '22 00:09

OnaBai