如何通过名称获取组件的对象?

时间:2014-12-02 17:58:03

标签: extjs

我有一个观点:

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我正在做form.up('id')。getForm()。

如何通过名称获取icon-p的对象,表明我可以隐藏或显示该图标。

2 个答案:

答案 0 :(得分:0)

您可以为项目分配ID并使用Ext.getCmp('id')来引用它

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

显示

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

隐藏它

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

我听说有人说为项目分配'id'可能效率不高(不确定为什么?)但这是测试你想要完成什么的好方法。也许你可以找到更好的方法,但这应该有用。

答案 1 :(得分:0)

在onPClick处理程序中,您将获得第一个参数中对组件的引用 在控制器的某个地方你有处理程序功能

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

当您将 itemId 分配给 icon-n对象时,您可以轻松获取参考资料, 你在onPClick处理程序中执行以下操作

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

完整的代码应该是

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
}