【问题标题】:How cell editing works with extjs store manager?单元格编辑如何与 extjs 商店管理器一起使用?
【发布时间】:2013-06-20 10:35:12
【问题描述】:

我有来自 Sencha's website

的示例代码
Ext.onReady(function() 
{   
    Ext.create('Ext.data.Store', {
        storeId:'simpsonsStore',
        fields:['name', 'email', 'phone'],
        data:{'items':[
            {"name":"Lisa", "email":"lisa@simpsons.com", "phone":"555-111-1224"},
            {"name":"Bart", "email":"bart@simpsons.com", "phone":"555-222-1234"},
            {"name":"Homer", "email":"home@simpsons.com", "phone":"555-222-1244"},
            {"name":"Marge", "email":"marge@simpsons.com", "phone":"555-222-1254"}
        ]},
        proxy: {
            type: 'memory',
            reader: {
                type: 'json',
                root: 'items'
            }
        }
    });

    Ext.create('Ext.grid.Panel', {
        title: 'Simpsons',
        store: Ext.data.StoreManager.lookup('simpsonsStore'),
        columns: [
            {header: 'Name',  dataIndex: 'name', editor: 'textfield'},
            {header: 'Email', dataIndex: 'email', flex:1,
                editor: {
                    xtype: 'textfield',
                    allowBlank: false
                }
            },
            {header: 'Phone', dataIndex: 'phone'}
        ],
        selType: 'cellmodel',
        plugins: 
        [{ 
            ptype: 'cellediting',
            clicksToEdit: 1
        }],
        height: 200,
        width: 400,
        renderTo: 'grid'
    });

});

这个ptype: 'cellediting' 插件允许通过单击textfield 进行内联单元格编辑。 我只是找不到关于如何以及在何处保存更改的值?我如何向单元格添加侦听器以便在每次更改后我都能够alert() strong> 新值?

感谢任何提示...

【问题讨论】:

    标签: javascript extjs grid store


    【解决方案1】:

    每次编辑器模糊(即失去焦点)时都会更新记录值。 Ext 会跟踪记录中已修改的字段,直到您提交更改(例如通过同步存储)。

    要收到已编辑字段的通知,请使用插件的edit 事件。正如您在文档中看到的,您可以直接在网格中为此事件安装监听器(无需专门将监听器添加到插件中)。

    编辑

    使用您的代码,将是这样的:

    Ext.create('Ext.grid.Panel', {
        title: 'Simpsons',
        store: Ext.data.StoreManager.lookup('simpsonsStore'),
        columns: [
            {header: 'Name',  dataIndex: 'name', editor: 'textfield'},
            {header: 'Email', dataIndex: 'email', flex:1,
                editor: {
                    xtype: 'textfield',
                    allowBlank: false
                }
            },
            {header: 'Phone', dataIndex: 'phone'}
        ],
        selType: 'cellmodel',
        plugins: 
        [{ 
            ptype: 'cellediting',
            clicksToEdit: 1
        }],
        height: 200,
        width: 400,
        renderTo: 'grid',
    
        listeners: {
            edit: function(editor, e) {
                var record = e.record;
    
                alert(Ext.String.format(
                    'The field "{0}" or record #{1} has been changed from {2} to {3}', 
                    e.field, record.get('id'), e.originalValue, e.newValue
                ));
    
                alert('The following fields of the records are dirty: ' + Ext.Object.getKeys(record.modified).join(', '));
            }
        }
    });
    

    【讨论】:

    • 太完美了。但是如果您的商店同步失败,您如何撤消上述编辑?因为虽然记录没有改变,但视觉上它仍然显示不正确的信息??我说的是一个网格检查列
    • 那是另一个问题,但是您将使用同步方法的失败回调,并在其中对每条记录调用reject。
    • 这正是我认为会撤消它的。实际上它确实撤消了记录值,但在网格上它仍然显示相反的(在 beforeedit 中设置的值。)
    • 您应该发布一个问题,并附上该问题的简短可复制示例。
    猜你喜欢
    • 2013-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-05
    • 2017-11-19
    • 1970-01-01
    • 2019-11-03
    • 2012-09-19
    相关资源
    最近更新 更多