How can I use nested Json to populate Kendo UI gri

2019-01-14 19:37发布

问题:

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.!

回答1:

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


回答2:

I just wanted to submit another answer based on OnaBai's.

http://jsfiddle.net/L6LwW/17/

The HTML:

<script id="message-template" type="text/x-kendo-template">
  #for (var i = 0; i
  < ddl.length; i++) {# <li><span>#=ddl[i].value#</li>
    #}#
    </script>

<div id="grid"></div>

The JS:

var grid = $("#grid").kendoGrid({
  dataSource: {
    data: [
      [{
        "id": 1,
        "name": "John Doe",
        "ddl": [{
          "key": 1,
          "value": "hello"
        }, {
          "key": 1,
          "value": "hello"
        }]
      }, {
        "id": 2,
        "name": "Don Joeh",
        "ddl": [{
          "key": 1,
          "value": "hello"
        }, {
          "key": 1,
          "value": "hello"
        }]
      }]
     ],
    pageSize: 10,
    schema: {
      parse: function(d) {
        for (var i = 0; i < d.length; i++) {
          if (d[i]) {
            return d[i];
          }
        }
        return [];
      }
    }
  },
  columns: [{
      field: "id",
      title: "ID"
    }, {
      field: "name",
      title: "Name"
    }, {
      field: "ddl",
      title: "DDL",
      width: "180px",
      template: kendo.template($("#message-template").html())
    } //template: "#=ddl.value#" }
  ]
}).data("kendoGrid");