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

后端 未结 2 672
轮回少年
轮回少年 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:17

    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");
    
    0 讨论(0)
  • 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");
    
    0 讨论(0)
提交回复
热议问题