0

我有 3 个按钮,当我单击按钮 3 时,我希望禁用按钮 1 和 2。我能够做到这一点。当我尝试为单击按钮 2 以禁用按钮 1 和 3 时设置另一个侦听器时,它无法正常工作。似乎它可能处于某种循环中。这是我在下面列出的代码:

{
                                text: 'Button 1',
                                name: 'button1',
                                cls: 'x-btn-text-icon',
                                listeners: {"click": function (){
                                    Ext.getCmp("button2").disable();
                                    Ext.getCmp("button3").disable();
                                    }
                                },
                                icon: 'public/images/edit.gif'
                            },{
                                text: 'Button 2',
                                name: 'button2',
                                cls: 'x-btn-text-icon',
                                listeners: {"click": function (){
                                    Ext.getCmp("button1").disable();
                                    Ext.getCmp("button3").disable();
                                    }
                                },
                                icon: 'public/images/edit.gif'
                            },{
                                text: 'Button 3',
                                name: 'button3',
                                cls: 'x-btn-text-icon',
                                listeners: {"click": function (){
                                    Ext.getCmp("button1").disable();
                                    Ext.getCmp("button2").disable();
                                    }
                                },
                                icon: 'public/images/edit.gif'
                            }

有没有更简单的方法来实现这一点?我很感激任何帮助。

4

3 回答 3

0

Ext.getCmp 用于id属性。您的按钮具有name属性,更改nameid并且您的示例应该可以工作。

于 2013-08-29T20:00:02.453 回答
0

我认为您可能正在寻找的是toggleGroup

于 2013-08-30T04:50:19.797 回答
0

我使用了替代方式,但我不确定是否符合您的需求。如果对您有意义,请在下面尝试。

  • 我已经在范围之外创建了一个数组来保留按钮 ID。
  • 然后做了一个功能来禁用按钮,除了点击。
  • 我使用按钮组来组织按钮
var buttons = new Array();

var disableButtons = function (btn) {
    // here it is to query button group by name
    var btns = Ext.ComponentQuery.query('buttongroup, [name=btnGroup]');
    var count = btns.length;

    for (var i = 1, x = 0; i < count; i++, x++) {
        this.buttons[x] = btns[i].id;
    }

    // this is the tricky part, disable all buttons except the clicked
    var index = this.buttons.indexOf(btn);
    if (index !== -1) {
        this.buttons.splice(index, 1);
    }

    Ext.Array.each(buttons, function (name) {
        Ext.getCmp(name).disable();
    })
}

// in any case, I created a function to enable all button status
// just add an other button in the scene and call this function
var enableButtons = function () {
    Ext.Array.each(buttons, function (name) {
        Ext.getCmp(name).enable();
    });
    // The following line necessary to keep array clean when user wants to disable
    // buttons again. The best way to empty an array, just set length to '0'
    buttons.length = 0;
}


// I used these functions for window component. When user click 'ADD' button, 
// buttons listener calls this function
{
text: 'EKLE',
scale: 'medium',
disabled: true,
width: 100,
margin: '0 9 0 0',
cls: 'btn-article-save',
id: 'btnArticleSave',
name: '_btnArticle',
listeners: {
    click: function () {
        var sel = Ext.getCmp('article-grid').getSelectionModel().getSelection();
        Ext.each(sel, function (item, index) {
            arrArticle.push(item);
        });

        arrArticleStore.load();
        Ext.getCmp('articles-grid').getView().bindStore(arrArticleStore);
        Ext.getCmp('articles-grid').show();

        var alevel = Ext.ComponentQuery.query('textfield[name=artLevel]');
        var avalue = Ext.ComponentQuery.query('textfield[name=artValue]');

        var artActions = [];
        for (var i = 0; i < alevel.length; i++) {
            artActions[i] = {'LEVELS': alevel[i].value, 'VALUES': avalue[i].value}
        }
        var count = winArticle.down('fieldset[id=article-fieldset]').items.items.length;
        for (i = 1; i <= count; i++) {
            var container = 'artContainer' + i;
            winArticle.down('fieldset[id=article-fieldset]').remove(container);
        }

        var qry = winArticle.query();
        for (var i = 0; i < qry.length; i++) {
            if (qry[i].xtype === 'textfield') {
                var txt = qry[i].id;
                Ext.getCmp(txt).reset();
                articles.loadData([], false);
            }
        }

        var _name = this.name;
        disableButtons(_name);
    }
}

}

于 2013-09-01T05:26:12.270 回答