How to enable/disable a forms button without Using Ext.getCmp() in extjs4?

12,859

Solution 1

You can use a controller to listen for the 'dirtychange' event fired by this form.

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

The answer darren gave will certainly work, this just utilizes component queries to give you a different way to reach and control components. If you wanted to enable a series of buttons within the form, for example, you could remove the 'text=Apply' and an array of all the forms buttons will be returned.

Solution 2

Use a constructor and then you can create the button inside it and then have a reference to it in your form. Once you have the reference in the form, you can then retrieve it from the listener you have there. Would look like this:

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

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

That should work without using Ext.getCmp()

Solution 3

Agreed with jthirau - definitely use component query.

Another way to do what you are doing is to simply add a button handler. This goes right on the button config:

handler:function(){
  //do something
}
Share:
12,859
Mehdi Fanai
Author by

Mehdi Fanai

Updated on June 04, 2022

Comments

  • Mehdi Fanai
    Mehdi Fanai almost 2 years

    Here is the following form and i wanna access form`s apply button inside the form without using Ext.getCmp() and defining an id for the button:

    {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,} ]}