简体   繁体   English

如何在ExtJS5中的网格编辑器组合的当前记录中添加项目

[英]How to add a item in current record of grid editor combo in ExtJS5

ExtJS 5 ExtJS 5

I have a grid and it has 3 columns (Id, Students,Selected Students). 我有一个网格,它有3列(Id,Students,Selected Students)。 In column2 (Students), I have bind static data. 在column2(学生)中,我绑定了静态数据。 When i click on any item of second column then this record should be added in column3 (Selected Students) in current record or row. 当我单击第二列的任何项目时,该记录应添加到当前记录或行的column3(选定的学生)中。 I have one button also called (Add new item) used for creating new row dynamically. 我有一个按钮,也称为(添加新项),用于动态创建新行。

Note - When i add a new row by clicking on Add new item button, then new row will be added and 3 column(Selected Students) value should be blank. 注意-当我通过单击“添加新项目”按钮添加新行时,将添加新行,并且3列(“选定学生”)值应为空白。

I have tried so much but didn't get solution. 我已经做了很多尝试,但是没有找到解决方案。 The main problem is that when i bind data in third column then it binds proper, but when i add a new row, it also shows in new record also but it should not be. 主要问题是,当我在第三列中绑定数据时,它会正确绑定,但是当我添加新行时,它也显示在新记录中,但实际上不应该。 If i clear store or combo item, then it removes from all rows instead of current record/row. 如果我清除存储或组合项目,那么它将从所有行中删除,而不是从当前记录/行中删除。

Ext.onReady(function () {

                var comboStore1 = Ext.create('Ext.data.Store',
                    {
                        fields: ['text', 'id'],
                        data:
                        [
                            { "text": "Value1", "id" :1 },
                            { "text": "Value2", "id": 2 },
                            { "text": "Value3", "id": 3 }
                        ]
                    });

                var comboStore2 = Ext.create('Ext.data.Store',
                    {
                        fields: ['text', 'id']
                    });

                var gridStore = Ext.create('Ext.data.Store',
                    {
                        fields: ['id', 'students', 'selectedStudents'],
                        data:
                        [
                            { "id" : 1},
                        ]
                    });               

                var window = new Ext.Window({
                    id: 'grdWindow',
                    width: 400,
                    height: 200,
                    items: [
                        {
                            xtype: 'panel',
                            layout: 'fit',
                            renderTo: Ext.getBody(),
                            items: [
                                {
                                    xtype: 'button',
                                    text: 'Add New Item',
                                    handler: function () {
                                        var store = Ext.getCmp('grdSample').store;

                                        var rec = {
                                            id: 1,
                                            students: '',
                                            selectedStudents: ''
                                        }
                                        store.insert(store.length + 1, rec);
                                    }
                                },
                                {
                                    xtype: 'grid',
                                    id: 'grdSample',
                                    store: gridStore,
                                    plugins: [
                                            Ext.create('Ext.grid.plugin.CellEditing', {
                                                clicksToEdit: 1
                                            })
                                            ],
                                    columns: [
                                        {
                                            header: 'id',
                                            dataIndex: 'id'
                                        },
                                        {
                                            header: 'Students',
                                            dataIndex: 'students',
                                            editor: {
                                                xtype: 'combobox',
                                                store: comboStore1,
                                                displayField: 'text',
                                                valueField: 'text',
                                                queryMode: 'local',
                                                listeners: {
                                                    select: function (combo, records) {
                                                        var rec = records[0].data;
                                                    }
                                                }
                                            }
                                        },
                                        {
                                            header: 'Selected Students',
                                            dataIndex: 'selectedStudents',
                                            editor: {
                                                xtype: 'combobox',
                                                id: 'combo2',
                                                store: comboStore2,
                                                displayField: 'text',
                                                valueField: 'id'
                                            }
                                        }
                                    ]
                                }
                            ]
                        }]
                }).show();
            });

I have tried almost everything but still i didn't get any solution. 我几乎尝试了所有方法,但仍然没有任何解决方案。 In another way - How to insert a value in grid editor combo only in current row. 换句话说-如何仅在当前行中在网格编辑器组合中插入值。 (Another row should not be reflected). (不应反映另一行)。 If another row is being reflected, then how to remove value before rendering from another row without reflecting other rows. 如果正在反映另一行,那么如何在从另一行进行渲染之前除去值而不反映其他行。

Well, I guess main problem is that you trying to change certain grid row editor component while it suppose to be same for all grid rows. 好吧,我想主要的问题是您试图更改某些网格行编辑器组件,而该组件假定对于所有网格行都是相同的。

The main problem is that when i bind data in third column then it binds proper, but when i add a new row, it also shows in new record also but it should not be. 主要问题是,当我在第三列中绑定数据时,它会正确绑定,但是当我添加新行时,它也显示在新记录中,但实际上不应该。 If i clear store or combo item, then it removes from all rows instead of current record/row. 如果我清除存储或组合项目,那么它将从所有行中删除,而不是从当前记录/行中删除。

It happens because all grid row editors use same store instance, comboStore2 , and when you change its data you change it for all editors. 发生这种情况是因为所有网格行编辑器都使用相同的存储实例comboStore2 ,并且当您更改其数据时,所有编辑器都会对其进行更改。

To create separate store for each editor you have to do something like this: 要为每个编辑器创建单独的存储,您必须执行以下操作:

{
    header: 'Selected Students',
    dataIndex: 'selectedStudents',
    editor: {
        xtype: 'combobox',
        id: 'combo2',
        store: Ext.create('Ext.data.Store', {
                fields: ['text', 'id']
        }),
        displayField: 'text',
        valueField: 'id'
    }
}

But than its become not trivial to select specific row editor component and its store. 但是,选择特定的行编辑器组件及其存储并不是一件容易的事。

I recommend you to take a look at Ext.grid.column.Widget as you can bind certain row (its record actually) to widget with its onWidgetAttach property. 我建议您看一下Ext.grid.column.Widget,因为您可以使用其onWidgetAttach属性将某些行(实际上是其记录)绑定到窗口小部件。

Your second column is dummy. 您的第二列是虚拟的。 You could directly use a tagfield component as the editor for your third column, which lets you select multiple values. 您可以直接将tagfield组件用作第三列的编辑器,从而可以选择多个值。

And you'll need a single store for the list of all students. 而且,您将需要一个商店来存储所有学生的列表。

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

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