简体   繁体   English

Kendo Grid:添加带有嵌套对象的新行停止工作

[英]Kendo Grid: add new row with nested object stopped working

I'm filling a Kendo data grid from nested JSON by this way: 我通过这种方式从嵌套JSON填充Kendo数据网格:

https://stackoverflow.com/a/24441318/535556 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 " “未捕获的TypeError:无法读取未定义的属性'street”

I would like to ask how to format the data properly to obtain nested JSON object with updated data? 我想问一下如何正确格式化数据以获得具有更新数据的嵌套JSON对象?

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. 当您添加新行而未为dataSource定义架构模型时,正在创建的对象还没有“ address”字段。 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. 带有“ address.street”的列正试图从新对象的“ address”字段中获取“ street”字段,该字段此时未定义,因此会出现错误。

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. 好消息是,您可以使用defaultValue为{}定义一个“地址”字段,并且Grid编辑器应该很高兴。

$("#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. 当然,“街道”,“城市”和“州”字段是未定义的,但是它们的父对象“地址”已定义,因此访问它们的字段时不会看到错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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