How to get object of component by its name?

佐手、 提交于 2020-01-17 11:35:11

问题


I have a view like:

tbar: [
        {
            iconCls:'icon-p',
            hidden: true,
            name:'p',
            handler:'onPClick'
        },
        {
            xtype: 'tbfill'
        },
        {
            iconCls:'icon-n',
            hidden:true,
            name: 'n',
            handler:'onNClick'
        }
    ],

    initComponent:  function(){
      Ext.apply(this, {
        layout: 'fit',  
        items: [
          {
            xtype:'textareafield',
            name: 'name_content'  
          }
        ]
      });
      this.callParent();
    }

OnPClick I am doing form.up('id').getForm().

How to get Object of icon-p by its name show that I can hide or show that icon.


回答1:


In the onPClick handler you get the reference to the component in the first parameter.
Somewhere in your controller you have the handler function

onPClick : function(icon-p-object){
  // do some stuff with the icon-p-object
}

When you assign an itemId to your icon-n object, then you can easily get the reference, when you do the follwoing in your onPClick handler

var parentComponent = icon-p-object.up(); // now we have the reference to the parent container
var icon-n-Component = parentComponent.down('#icon-n'); // and with that we can get the other reference of the child element

The full code should be

onPClick : function(icon-p-object){
  var parentComponent = icon-p-object.up();
  var icon-n-Component = parentComponent.down('#icon-n');
  // do some stuff with the icon-n object
}



回答2:


You can assign an id to the item and use Ext.getCmp('id') to reference it

    {
        iconCls:'icon-p',
        hidden: true,
        name:'p',
        handler:'onPClick',
        id: 'p_id'
    },

To show it

Ext.getCmp('p_id').show();

To hide it

Ext.getCmp('p_id').hide();

I've heard people say that assigning 'id' to items can be inefficient (not sure why?) but this is a good way to test what you are trying to accomplish. Perhaps you can find a better way, but this should work.



来源:https://stackoverflow.com/questions/27255929/how-to-get-object-of-component-by-its-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!