Extjs4 获取items子控件时使用Ext.getCmp返回undefined

来源:互联网 发布:nodejs获取windows ip 编辑:程序博客网 时间:2024/05/19 14:38

注意:使用的Extjs版本为4.2.1

我的情况发生在一个组件的子控件中获取另一个组件的子控件对其做修改时。

举例:比如panelA的items中的buttonA,获取panelB的items中的labelfieldB并修改其值,在buttonA的listeners或handler函数中使用Ext.getCmp("labelfieldB")只能返回undefined。

组件items配置中的子控件的id配置是使用"itemId",在API中找到该配置项,可以看到介绍里说道:

Instead of using an id with Ext.getCmp, use itemId with Ext.container.Container.getComponent which will retrieve itemId's or id's. Since itemId's are an index to the container's internal MixedCollection, the itemId is scoped locally to the container -- avoiding potential conflicts with Ext.ComponentManager which requires a unique id.

从上面可以看到子控件的itemId的作用域是它的容器,要通过子控件的itemId或者id获取控件要使用容器的getComponent方法。

所以最上面的例子中,buttonA获取labelfieldB的方法应该是:

Ext.getCmp("panelB").getComponent("labelfieldB")  或者

panelB.getComponent("labelfieldB")

原创粉丝点击