20

I examined how ExtJs renders form's fields in dom. To change fieldLabel after field is rendered I find proper dom element and change its innerHTML;

/**
* Modifies field's label afrer field is rendered.
*
* @param {object} field
*    Some object that directly or indirecty extends Ext.form.Field
* @param {String} label
*    New value for field's label.
*/
function setLabel(field,label){
    var el = field.el.dom.parentNode.parentNode;
    if( el.children[0].tagName.toLowerCase() === 'label' ) {
        el.children[0].innerHTML =label;
    }else if( el.parentNode.children[0].tagName.toLowerCase() === 'label' ){
    el.parentNode.children[0].innerHTML =label;
    }
    return setLabel; //just for fun
}

//EXAMPLE:
var win = new Ext.Window({
    height : 200,
    width : 300,
    layout : 'form',
    labelAlign : 'right',
    items : [{
        xtype : 'textfield',
        fieldLabel : 'name',
        ref : 'f',
        html : 'asdf'
    },{
        xtype : 'datefield',
        fieldLabel : 'date',
        ref : 'd'
    },{
        xtype : 'combo',
        fieldLabel : 'sex',
        ref : 'c',
        store : [[1,"male"],[2,"female"]]
    },{
        xtype : 'radio',
        fieldLabel : 'radio',
        ref : 'r'
    },{
        xtype : 'checkbox',
        fieldLabel : "checkbox",
        ref : 'ch'
    }]
}).show()

setTimeout(function(){
   setLabel(win.f,'Last Name')(win.d,'Birth Date')(win.c,'Your Sex')(win.r,'jus radio')(win.ch,'just checkbox');
},3000);
Zango
  • 2,387
  • 3
  • 19
  • 33

6 Answers6

23

Accessing the dom to dynamically change fieldLabels was pre 3.0.1

After 3.0.1 I believe this works

field.labelEl.update('New label');

You can only do this once the field has been rendered.

David Young
  • 4,643
  • 2
  • 21
  • 18
12

If you want to add a label with HTML the best solution for ExtJs 4 is with update method (update method is in labelEl, not in label):

field.labelEl.update('New label');

Juan Ramón
  • 188
  • 1
  • 2
  • 10
  • This worked fine. However, how do I get the label value after that? Calling field.getFieldLabel() returns the OLD value :-/ – Geo May 28 '13 at 17:31
5

With ExtJS 4.2 this works for me:

field.setFieldLabel('New Label');

Klors
  • 2,665
  • 2
  • 25
  • 42
edinhow
  • 51
  • 1
  • 2
5

I've just come across the same issue, but in my situation the labels could already be rendered, or not. so I had to cover both cases:

var newLabel = 'new label';
if (!field.rendered) field.fieldLabel = newLabel;
else field.label.update(newLabel);

I tested it in ExtJS 3.2

NDM
  • 6,731
  • 3
  • 39
  • 52
5

couldn't find label property in a field object in Ext 4 instead this worked for me:

field.labelEl.dom.innerText = 'New Label';
Maruccio
  • 509
  • 1
  • 6
  • 14
0

In Extjs 3.4.0, the first line in the function doesn't work.

Replace with this:

//var el = field.el.dom.parentNode.parentNode;
var el = Ext.getCmp(field).getEl().dom.parentNode.parentNode;
mdml
  • 22,442
  • 8
  • 58
  • 66
Exe
  • 1