1
votes

I have this json object

{
  id: string,
  name: string,
  category: {
    id: string
    name: string,         
  }
}

I want to have column that bind to productCategory.name. However that field is nullable. When productCategory is null/undefined, kendo will throw error. How can i tell kendo that if field is undefined, just show empty string?

EDIT

Below is my sample data

[{
   "id":1,
   "name":"Spaghetti",
   "category":{
      "id":"1",
      "name":"Food"
}},
{
   "id":2,
   "name":"Coca-cola",
   "category":null
}}]

Below is my kendo datasource

var kendoDataSource = new kendo.data.DataSource({
        schema: {
            data: "data",
            total: "total",
            model: {
                id: "id",
                fields: {
                    id: { type: "string" },
                    name: { type: "string" },
                    "category.name": { type: "string" }                
                }

            }
        }
    });

Data above will throw "undefined" error, because second product does not have category.

2
Could you please provide your code?Jayesh Goyani
You have a column to show an object?DontVoteMeDown

2 Answers

0
votes

Try using empty object instead of null

created a fiddle,check this:

http://jsfiddle.net/Sowjanya51/h930jcru/

0
votes

Just provide a default value in your model like this:

var kendoDataSource = new kendo.data.DataSource({
        schema: {
            data: "data",
            total: "total",
            model: {
                id: "id",
                fields: {
                    id: { type: "string" },
                    name: { type: "string" },
                    "category.name": { type: "string" },
                    category: { defaultValue: {
                                                id: "",
                                                name: "",         
                                              }
                              }
                }

            }
        }
    });

This will initialize productCategory if it is null or undefined.