若你要取得相关的Component 若是属于container 请设置id
若是只属于某个container 底下的元件,请设置itemId;某些情况下当然你也可以连同id 一起设置,比如需要改变css style,除此之外itmeid 还是优先的选择
getCmp 以及getComponent 是不一样的,使用上必须注意,必须搭配id 以及itemid 使用
如此一来id 与itemid 就会有从属关系,非必要不需定义id,也不会造成在定义id 时需要编码避免重覆,而因为itemid 是属于某个id 底下的,所以即使itemid 重覆,也可以利用唯一的id 利用Ext.getCmp 取得参照后,在接着使用getComponent 取得所属itemId 的参照。
怎么获取id和itemId的组件和元件(从以上可以明显的看出,itemId是个局部的元件名称):
id: Ext.getCmp(id);
itemId:Ext.getCmp(id).getComponent(itemId);
再看一段代码
var c = new Ext.panel.Panel({ height: 300, renderTo: document.body, layout: 'auto', items: [ { itemId: 'p1', title: 'Panel 1', height: 150 }, { itemId: 'p2', title: 'Panel 2', height: 150 } ] }) p1 = c.getComponent('p1'); p1 = c.child('#p1'); p2 = p1.ownerCt.getComponent('p2'); p2 = p1.ownerCt.child('p2');
你可以想象下,如果你再创建了一个c呢,id就重复了。
我们可以简单的理解为id是全局变量,itemid是局部变量