【问题标题】:Extjs Cellediting onclick of a buttonExtjs Cellediting onclick 一个按钮
【发布时间】:2015-06-10 17:56:17
【问题描述】:

我在网格的每一行都有激活按钮,因此用户只能激活他们想要编辑的行,并且按钮变为停用。我如何保存按钮的状态以备下次重新打开面板时使用?下面是我的代码,也是:https://fiddle.sencha.com/#fiddle/ofp

Ext.application({
name : 'Fiddle',

launch : function() {         

    Ext.create('Ext.data.Store', {
        storeId:'simpsonsStore',
        fields:['name', 'email', 'phone'],
        data:{'items':[
            {"id": 1, "name":"Lisa", "email":"lisa@simpsons.com", "phone":"555-111-1224"},
            {"id": 2, "name":"Bart", "email":"bart@simpsons.com", "phone":"555--222-1234"},
            {"id": 3, "name":"Homer", "email":"home@simpsons.com", "phone":"555-222-1244"},                        
            {"id": 4, "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: 'textfield'},
            {header: 'change status',  renderer: extjsRenderer},
            {header: 'Phone', dataIndex: 'phone', editor: 'textfield'}
        ],
        height: 200,
        width: 600,
        renderTo: Ext.getBody(),
        selType: 'rowmodel',
        plugins: [
            Ext.create('Ext.grid.plugin.CellEditing', {
                clicksToEdit: 1,
                listeners: {
                    beforeEdit: function(editor, e) {
                        var button = Ext.getCmp('editButton_' + e.record.get('id'));

                        if (button.active)
                            return true;
                        else
                            return false;
                    }
                }
            })
        ]
    });

激活/停用按钮代码:

    function extjsRenderer(val,meta,rec) {        
    var id = Ext.id();
    Ext.defer(function () {

            Ext.widget('button', {
                renderTo: id,
                id:'editButton_' + rec.get('id'),
                text: 'Activate',
                width: 75,

        handler: function() {
            if (this.active == null || this.active == false) {
                this.active = true
                this.setText('Deactivate');
                return;
            }

            if (this.active == true) {
                this.active = false;
                this.setText('Activate');
                return;
            }
        }
            });
    //    } 
    }, 50);
    return Ext.String.format('<div id="{0}"></div>', id);
}
}
});

【问题讨论】:

    标签: javascript extjs


    【解决方案1】:

    这很简单。我为你做了一个小小提琴:https://fiddle.sencha.com/#fiddle/ods

    ExtJS 组件就像简单的对象字面量一样工作,所以你可以用任何你想要的东西来扩展它们。在这种情况下,我添加了可以在 beforeEdit 监听器中检查的属性“active”。

    【讨论】:

    • 非常感谢您的帮助。实际上,我稍微修改了您的代码,因为我需要每行中的按钮也将行编辑更改为单元编辑,但它无法正常工作,您能检查一下并提供帮助吗? fiddle.sencha.com/#fiddle/ofp
    • 看看我上面链接的小提琴。每个按钮都需要一个唯一的 ID。我在 json 中添加了一个 id 属性,以标识将启用哪一行进行编辑。
    • 非常感谢,它成功了!我只需要保存状态。目前,当我重新打开面板时,所有按钮都重置为激活。那么如果之前激活了,我该如何保存以备下次使用呢?
    • 您可以使用属性(例如“activeedit”)将激活状态存储在存储中,并检查存储的状态。从按钮中删除激活属性并使用record.set('activeedit, true) 编辑商店项目;我又更新了小提琴......
    猜你喜欢
    • 1970-01-01
    • 2013-05-14
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-07
    • 2017-08-06
    • 1970-01-01
    相关资源
    最近更新 更多