Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have JSON with structure like this:

"id":1,
"user_role":"ADMIN",
"state":"ACTIVE",
"address":{
   "street":"test 59",
   "city":"City test",
   "post_number":"25050"
},

How I should to pass values of address.street into column using setting in fields and model?

Many thanks for any advice.

like image 812
redrom Avatar asked Jun 26 '14 13:06

redrom


People also ask

What is the Kendo UI grid?

The Kendo UI Grid is a powerful widget that allows you to visualize and edit data via its table representation. It provides many options, such as paging, sorting, filtering, grouping, and editing, which determine the way data is presented and manipulated.

How to bind data to kendo grid using DataSource?

The Grid can be bound to local or remote data by using the Kendo UI DataSource component. Bind data to Kendo Grid by using AJAX Read action method. Change the datasource on change event of any HTML controls. Normally, a developer can bind the data to Grid by using AJAX Read method.

How to bind the data to grid in MVC?

Normally, a developer can bind the data to Grid by using AJAX Read method. This read method is ActionResult method in MVC which will return JSON DataSourceResult & direclty bind the data to Grid.


2 Answers

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/

like image 135
OnaBai Avatar answered Oct 19 '22 03:10

OnaBai


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

like image 24
Tom Avatar answered Oct 19 '22 05:10

Tom