对象值的 Celleditor Extjs4

发布于 2025-01-08 06:04:27 字数 1278 浏览 4 评论 0原文

我正在寻找如何做到这一点的最佳解决方案。 我所拥有的:

// model
Ext.define("User", {
    extend: "Ext.data.Model",
    fields: [
        {name: "id", type: "int"},
        {name: "name"},
        {name: "description", type: "string"}
    ]
});
// store with data
var oStore = new Ext.data.Store({
    model: "User",
    data: [
        {id: 1, name: {name:"John"}, description: "Fapfapfap"},
        {id: 2, name: {name:"Danny"}, description: "Boobooboo"},
        {id: 3, name: {name: "Tom"}, description: "Tralala"},
        {id: 4, name: {name:"Jane"}, description: "Ololo"},
    ]
});
// and finally I have a grid panel
Ext.create("Ext.grid.Panel", {
    columns: [
        {dataIndex: "id", header:"ID"},
        {
            dataIndex: "name", 
            header: "Name", 
            renderer: function(value){return value.name;}, 
            editor: "textfield"},
        {dataIndex: "description", header: "Description", flex: 1, editor: "htmleditor"}
    ],
    plugins: [new Ext.grid.plugin.CellEditing({clicksToEdit: 2})],
    store: store,
    renderTo: document.body
});​

当我双击单元格时,我会在编辑器字段中看到 [object] Object ,当我输入有效值时,我会在网格中看到空单元格。

所以,问题是 - 我如何设置 celleditor 不从 record.name 而是从 record.name.name 获取数据?

I'm looking for a best solution how to do this.
What I have:

// model
Ext.define("User", {
    extend: "Ext.data.Model",
    fields: [
        {name: "id", type: "int"},
        {name: "name"},
        {name: "description", type: "string"}
    ]
});
// store with data
var oStore = new Ext.data.Store({
    model: "User",
    data: [
        {id: 1, name: {name:"John"}, description: "Fapfapfap"},
        {id: 2, name: {name:"Danny"}, description: "Boobooboo"},
        {id: 3, name: {name: "Tom"}, description: "Tralala"},
        {id: 4, name: {name:"Jane"}, description: "Ololo"},
    ]
});
// and finally I have a grid panel
Ext.create("Ext.grid.Panel", {
    columns: [
        {dataIndex: "id", header:"ID"},
        {
            dataIndex: "name", 
            header: "Name", 
            renderer: function(value){return value.name;}, 
            editor: "textfield"},
        {dataIndex: "description", header: "Description", flex: 1, editor: "htmleditor"}
    ],
    plugins: [new Ext.grid.plugin.CellEditing({clicksToEdit: 2})],
    store: store,
    renderTo: document.body
});​

When I doublecick on a cell I see [object] Object in editor's field, and when I enter valid value than I see empty cell in the grid.

So, the question is – how could I setup celleditor to get data not from record.name but from record.name.name?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傲性难收 2025-01-15 06:04:27

您可以在模型上重写 getset 方法,因此将支持多级字段名称。下面是示例实现。

Ext.define("User", {
    extend: "Ext.data.Model",
    fields: [
        {name: "id", type: "int"},
        {name: "name"},
        {name: "description", type: "string"}
    ],
    get: function(key) {
        if (Ext.isString(key) && key.indexOf('.') !== -1) {
            var parts = key.split('.');
            var result = this.callParent([ parts[0] ]);
            return result[parts[1]];
        }
        return  this.callParent(arguments);
    },
    set: function(key, value) {
        if (Ext.isString(key) && key.indexOf('.') !== -1) {
            var parts = key.split('.');
            var result = this.get(parts[0]);
            result[parts[1]] = value;

            this.callParent([ parts[0], result ]);
            return;
        }
        this.callParent(arguments);
    }
});

我不确定商店是否检测到对 name.name 字段进行的更改。如果不是,您可能还应该将记录标记为脏。

工作示例: http://jsfiddle.net/lolo/dHhbR/2/

You can override get and set methods on model, so the will support multi-level field names. Below is sample implementation.

Ext.define("User", {
    extend: "Ext.data.Model",
    fields: [
        {name: "id", type: "int"},
        {name: "name"},
        {name: "description", type: "string"}
    ],
    get: function(key) {
        if (Ext.isString(key) && key.indexOf('.') !== -1) {
            var parts = key.split('.');
            var result = this.callParent([ parts[0] ]);
            return result[parts[1]];
        }
        return  this.callParent(arguments);
    },
    set: function(key, value) {
        if (Ext.isString(key) && key.indexOf('.') !== -1) {
            var parts = key.split('.');
            var result = this.get(parts[0]);
            result[parts[1]] = value;

            this.callParent([ parts[0], result ]);
            return;
        }
        this.callParent(arguments);
    }
});

I am not sure if store detects change made to name.name field. If no, you should also probably mark record as dirty.

Working sample: http://jsfiddle.net/lolo/dHhbR/2/

迷迭香的记忆 2025-01-15 06:04:27

编辑器接受列的“dataIndex”字段中提供的任何值。由于“名称”是一个对象,因此这就是您所得到的。在编辑器中输入名称后,值等于字符串(而不是对象),并且渲染器正在尝试获取该字符串的 name 属性。

解决此问题的最简单方法是将商店的“名称”字段设置为字符串而不是对象。但是,我假设您想这样做是有原因的。

CellEditing 插件可以监听三个事件:beforeedit、edit 和 validateedit。您可以实现一个 beforeedit 侦听器以从列中获取“name”对象,然后获取该对象的“name”属性并用该值填充编辑器。然后在 validateedit 上,从编辑器获取值,并使用该值设置记录中“name”对象的“name”属性。

为了快速参考,这里是事件定义: 单元格编辑事件

The editor accepts whatever value is provided in the "dataIndex" field of the column. Since "name" is an object, that's what you're getting. After entering a name in the editor, value is equal to a string (not an object) and your renderer is trying to get the name property of the string.

The easiest way to fix this is to make the "name" field of your store a string instead of an object. However, I'm assuming there's a reason you want to do it this way.

The CellEditing plugin has three events it can listen for: beforeedit, edit, and validateedit. You can implement a beforeedit listener to get the "name" object from the column, then get the "name" property of that object and fill the editor with that value. Then on validateedit, get the value from the editor and set the "name" property of the "name" object in the record with that value.

For quick reference, here's the event definition: CellEditing events

甲如呢乙后呢 2025-01-15 06:04:27

一种更简单的方法是修改用户模型对象以不同方式映射“名称”属性:

{name: "name", mapping:'name.name'}

然后其他一切保持不变。

An easier way is to modify your User Model object to map the "name" property differently:

{name: "name", mapping:'name.name'}

then everything else stays the same.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文