Using Id and itemId in Extjs to access components

user1058913 picture user1058913 · Apr 2, 2015 · Viewed 27.3k times · Source

In ExtJs Best practices I gone through not to use Id for accessing Ext Components rather use ItemId, I am very new in accessing components using ItemID, does any one can help me in default syntax or the way to access components.

Also on click of yes in a message box I need to disable some components in masked page, whether this can be achieved with the help of ItemID? Please explain.

I feel when using ItemiD it may return array of elements/components, so if need to get an exact component I need to iterate again. I have this ques too....

Answer

Sujan Thakare picture Sujan Thakare · Apr 7, 2015

Basic difference between id and itemId is

When you use an id for a component, there must be a single instance of this component, If you create another instance that has the same id, you will have problems as the DOM is confused.

when you use itemId, it should be unique only within the component's immediate container.the component's container maintains a list of children ids.

so the best practice is to use itemId instead of id

now How to access?

if you use id

Ext.getCmp('id')

or

document.getElementById('id')

or

Ext.ComponentQuery.query("#id")[0]

if you use itemId

parentContainer.getComponent('child_itemId'),

refer following example

e.g

 var parentContainer= Ext.create('Ext.panel.Panel', {
         initComponent: function(){
            Ext.applyIf(me, {
               //childrens
               items: [{
                          xtype:'textfield',
                          itemId:'text'
                       },
                       {
                           xtype:'panel',
                           itemId:'childpanel',
                           items:[
                                 {
                                     xtype:'combobox',
                                     itemId:'combo'
                                 }
                           ]
                       }]
            });
            this.callParent(arguments);
        },
        renderTo:Ext.getBody()

    })

in above example

for accessing textfield use

parentContainer.getComponent('text');

for accessing combobox use

parentContainer.getComponent('childpanel').getComponent('combo');

or

Ext.ComponentQuery.query("#combo")[0];

this will return array of item with id combo in page for these you should use unique itemId so you will get the first item you are searching for

or

parentContainer.queryById('combo');

you can also use Ext.util.MixedCollection

        var fields = new Ext.util.MixedCollection();
        fields.addAll(parentContianer.query('[isFormField]'));
        var Combo = fields.get('combo');