如何在extjs4中不使用Ext.getCmp()启用/禁用表单按钮?

时间:2012-02-03 19:56:49

标签: extjs extjs4

以下是以下表单,我想在表单中访问表单的应用按钮,而不使用Ext.getCmp()并为按钮定义id:

{xtype : 'form',
url : 'index.php/submitform',
trackResetOnLoad : true,
id : 'generalprofilebasicinformation'+ this.getId(),
listeners : {
//is fired when the form is dirty(field values are modified)
    dirtychange : {
        fn : function(sm) {
    //Here Is my problem:
//How to  access to Apply button or any other buttons defined inside the form???
            var applyButton = this.Button;
   applyButton.enable();}}},
   buttons : [{
            text : 'Apply',
            formBind : true, // only
            // enabled
            // once the
            // form is
            // valid
            disabled : true,} ]}

3 个答案:

答案 0 :(得分:2)

您可以使用控制器来侦听此表单触发的“dirtychange”事件。

//controller init 
init: function() {
    this.control({
        'form': {
            dirtychange: function(form) {
                var button = form.owner.query('button[text=Apply]');
                button[0].enable();             
            }
        }
    });
}

darren给出的答案肯定会有效,这只是利用组件查询为您提供了一种不同的方式来访问和控制组件。例如,如果要在表单中启用一系列按钮,则可以删除“text = Apply”,并返回所有表单按钮的数组。

答案 1 :(得分:1)

使用构造函数,然后您可以在其中创建按钮,然后在表单中引用它。一旦你在表单中有了引用,你就可以从你在那里的监听器中检索它。看起来像这样:

contructor: function(config) {
   this.button = new Ext.Button({
       // Provide your button config here
   });
}

listeners: {
   dirtychange: function(sm) {
       this.button.enable();
   }
}

这应该可以在不使用Ext.getCmp()

的情况下工作

答案 2 :(得分:1)

同意jthirau - 绝对使用组件查询。

另一种做你正在做的事情的方法是简单地添加一个按钮处理程序。这在按钮配置上正确:

handler:function(){
  //do something
}