Kendo data grid - how to set column value from nested JSON object?

本小妞迷上赌 提交于 2019-11-29 07:03:39

If you want to show all values in a single column do what @RobinGiltner suggests.

If you want to show each member of address in a different column you can do:

var grid = $("#grid").kendoGrid({
    dataSource: data,
    editable: true,
    columns   : [
        { field: "id", title: "#" },
        { field: "user_role", title: "Role" },
        { field: "address.street", title: "Street" },
        { field: "address.city", title: "City" },
        { field: "address.post_number", title: "Post#" }
    ]
}).data("kendoGrid");

i.e.: use address.street as name of the field. This would allow you even to edit the field as in the example: http://jsfiddle.net/OnaBai/L6LwW/

@OnaBai Good and intuitive answer. Sadly Kendo doesn't always work to well with nested properties this way. For example formating doesn't work. Here is an example using data source shema to access nested properties. This way you can use formatting but you have to specify a schema model.

  var grid = $("#grid").kendoGrid({
  dataSource: {
    data: data,
    schema: {
      model: {
        id: "id",
        fields: {
          id: { type: "number" },
          user_role: { type: "string" },
          address_street: { from: "address.street" },
          address_city: { from: "address.city" },
          address_post_number: {
            type: "number",
            from: "address.post_number"
          }
        }
      }
    }
  },
  columns: [{
    field: "id",
    title: "#"
  }, {
    field: "user_role",
    title: "Role"
  }, {
    field: "address_street",
    title: "Street"
  }, {
    field: "address_city",
    title: "City"
  }, {
    field: "address_post_number",
    title: "Post#",
    format: "{0:0#######}"
  }]
}).data("kendoGrid");

Jsfiddle: http://jsfiddle.net/wtj6mtz2

See also this Telerik example for accessing nested properties.

You could use a template on the grid column definition to display whichever pieces of the address you wanted.

{ field: 'address', title: 'Address', template: '#= address.street#  #= address.city#, #= address.post_number# ' },

See documentation for kendo column template. http://docs.telerik.com/kendo-ui/api/web/grid#configuration-columns.template

See sample at http://jsbin.com/gizab/1/edit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!