Kendo Grid: add new row with nested object stopped working

心不动则不痛 提交于 2020-02-05 07:33:05

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/24446208/kendo-grid-add-new-row-with-nested-object-stopped-working

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