简体   繁体   中英

Kendo Grid: add new row with nested object stopped working

I'm filling a Kendo data grid from nested JSON by this way:

https://stackoverflow.com/a/24441318/535556

Everything works fine until I click on the "Add new row" button.

Then I receive a console error message:

"Uncaught TypeError: Cannot read property 'street' of undefined "

I would like to ask how to format the data properly to obtain nested JSON object with updated data?

Many thanks for any advice.

When you add a new row without having defined a schema model for the dataSource, the object being created does not yet have an "address" field. The column with "address.street" is attempting to get the "street" field from the "address" field of the new object which is undefined at this point, hence the error.

The bad news is that the schema model definition doesn't really lend itself to nested types. The good news is that you can define an "address" field with defaultValue of {} and the Grid editor should be happy.

$("#myGrid").kendoGridEx({

    ...

    columns: [
        { field: "address.street" },
        { field: "address.city" },
        { field: "address.state" },

        ...

    ],

    dataSource: new kendo.data.DataSourceEx({

        ...

        schema: {
            model: {
                id: "Id",
                fields: {
                    address: { defaultValue: {} },
                },
            },
        },

        ...
    }),

});

Now when you add a new row, the bound object's "address" field will be {}. The "street", "city" and "state" fields will of course be undefined, but their parent object "address" IS defined so you won't see and error when accessing it's fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM