javascript ExtJs 4,有没有办法找到控件?(不是通过“id”)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5789183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ExtJs 4, is there a way to find control? (not by "id")
提问by obenjiro
I have button with id = 'someid'
, i can find it by Ext.getCmp('someid')
...
我有按钮id = 'someid'
,我可以通过Ext.getCmp('someid')
...找到它
Is there a way to set some kind of "extraId" to that button, and find that button by "extraId'?
有没有办法为那个按钮设置某种“extraId”,然后通过“extraId”找到那个按钮?
回答by SimonSimCity
The question is where you want to search to get your item.
问题是您想从何处搜索以获取您的物品。
Another way could be the itemId.
另一种方式可能是itemId。
An itemId can be used as an alternative way to get a reference to a component when no object reference is available. 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 可用作获取组件引用的替代方法。不要将 id 与 Ext.getCmp 一起使用,而是将 itemId 与 Ext.container.Container.getComponent 一起使用,这将检索 itemId 或 id。由于 itemId 是容器内部 MixedCollection 的索引,因此 itemId 在容器的本地范围内 - 避免与需要唯一 ID 的 Ext.ComponentManager 发生潜在冲突。
Source and further documentation: http://docs.sencha.com/ext-js/4-0/#/api/Ext.AbstractComponent-cfg-itemId
来源和进一步的文档:http: //docs.sencha.com/ext-js/4-0/#/api/Ext.AbstractComponent-cfg-itemId
The ComponentQuery(mentioned by wombleton) is also a good way but maybe not as performant as the itemId.
该ComponentQuery(由wombleton提到的)也是一个不错的办法,但也许并不像高性能的的itemId。
回答by wombleton
You can search for it by doing an Ext.ComponentQuery
.
您可以通过执行Ext.ComponentQuery
.
Example that will work in the console on the linked page:
将在链接页面上的控制台中工作的示例:
Ext.ComponentQuery.query('button[text=OK]')
You can replace text=OK
with the attribute you are searching for.
您可以替换text=OK
为您正在搜索的属性。
回答by It Grunt
I don't have access to the ExtJS 4 API docs, but I know that in 3.X and previous verions have some other ways of getting references to your compoents without the use of the DOM ID.
我无权访问 ExtJS 4 API 文档,但我知道在 3.X 和以前的版本中有一些其他方法可以在不使用 DOM ID 的情况下获取对组件的引用。
If your components are part of a form or form panel, you should have access to a method called findField(). It is inherited from Basic Form. This method can take an id, dataIndex, name, or hiddenName to get a reference to your component.
如果您的组件是表单或表单面板的一部分,您应该可以访问名为 findField() 的方法。它继承自 Basic Form。此方法可以使用 id、dataIndex、name 或 hiddenName 来获取对组件的引用。
There is also a helper method called findByType(xtype) that you can use to try and find your buttons. Another option would be to extend the button you use to place a unique property that you can use as a reference.
还有一个名为 findByType(xtype) 的辅助方法,您可以使用它来尝试查找您的按钮。另一种选择是扩展您用来放置可用作参考的唯一属性的按钮。
回答by Jitendra
An alternative to id is itemId and it is limited to the scope of the container in which it is defined. So, in this case, there is not need to define globally unique itemId as done in case of id. We can use component query to get the component object having the itemId. Please follow this link where i have posted about how to use itemId and what is its benefit. http://jksnu.blogspot.in/2012/11/use-itemid-instead-of-id.html
id 的替代方法是 itemId,它仅限于定义它的容器的范围。因此,在这种情况下,不需要像 id 那样定义全局唯一的 itemId。我们可以使用组件查询来获取具有 itemId 的组件对象。请点击我发布的关于如何使用 itemId 及其好处的链接。 http://jksnu.blogspot.in/2012/11/use-itemid-instead-of-id.html