如何在 ExtJS TreePanel 中找到选定的节点?
当用户单击按钮时,我试图检索 TreePanel 的选定节点(如果有)。 如何检索 TreePanel 中的选择节点? 谢谢。
I'm trying to retrieve the selected node, if any, of a TreePanel when the user clicks a button. How do you retrieve the select node in a TreePanel? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
这是针对 ExtJS4 TreePanel 的
This is for ExtJS4 TreePanel
对于 ExtJS 4...
我在树面板中添加了以下侦听器:
其中 selected_node 是全局变量。 诸如追加和删除之类的函数可以与此记录变量一起使用,例如:
我已经创建了添加节点和删除节点的按钮,它们使用上面的按钮向选定的节点追加和删除节点。 remove() 将删除选定的节点以及所有子节点!
您还可以随时使用以下方法获取选定的节点(通过单击鼠标左键和右键进行选择):
var selected_node = Ext.getCmp('tree_id').getSelectionModel().getSelection()[0];
For ExtJS 4...
I have added the following listener in my tree panel:
where selected_node is a global variable. Functions like append and remove can be used with this record variable e.g.:
I have created buttons for Add Node and Delete Node which use the above to append and remove nodes to the selected node. remove() will remove the selected node as well as all child nodes!
You may also get the selected node any time using (the selection occurs with left as well as right mouse click):
var selected_node = Ext.getCmp('tree_id').getSelectionModel().getSelection()[0];
@Steve
Ext.fly('navTree').getSelectionModel().getSelectedNode();
不起作用,请使用
Ext.getCmp('navTree').getSelectionModel().getSelectedNode ();
代替。
@Steve
Ext.fly('navTree').getSelectionModel().getSelectedNode();
is not going to work, use
Ext.getCmp('navTree').getSelectionModel().getSelectedNode();
instead.
在 ExtJS4 中,您可以使用 getLastSelected() 方法,该方法返回树中最后一个选定的项目。
请参阅 ExtJS: http:// /docs.sencha.com/ext-js/4-0/#!/api/Ext.selection.Model-method-getLastSelected
示例可能如下所示:
In ExtJS4 you can use the getLastSelected() method which returns the last selected item in a tree.
See ExtJS: http://docs.sencha.com/ext-js/4-0/#!/api/Ext.selection.Model-method-getLastSelected
An example might look like this:
简单的 ....
Simple ....
您要做的就是创建一个事件处理程序。 每个 ExtJs 对象都有许多自动与其关联的事件。 您将编写一个事件处理程序(函数),然后将其分配给事件侦听器。 因此,在这种情况下,您可能希望为 TreePanel 组件的“click”事件分配一个事件处理程序。
但是,如果您想知道已选择的节点,会发生什么情况? 此时,您需要访问 TreePanel 的选择模型。 您提到了按钮操作。 假设您想要向该按钮的单击处理程序应用一个函数来获取选定的节点:
您使用 Flyweight 元素来获取对 TreePanel 本身的快速引用,然后使用 TreePanel 的内部方法来获取它的选择模型。 之后,您使用该选择模型(在本例中为 DefaultSelectionModel)的内部方法来获取选定节点。
您将在 Ext JS 文档中找到大量信息。 在线(和离线 AIR 应用程序)API 相当广泛。 即使您不直接使用 Core,Ext Core 手册也可以让您深入了解 ExtJS 开发。
What you would do is create an event handler. Each ExtJs object has a number of events that are automatically associated with them. You would write an event handler (a function) that you could then assign to an event listener. So, in this case, you would probably want to assign an event handler to the 'click' event of your TreePanel component.
But, what happens if you want to know a node that is already selected? At that point you'll need to access the TreePanel's Selection Model. You mentioned a button action. Let's say you wanted to apply a function to that button's click handler to get the selected node:
You used the flyweight element to get a quick reference to the TreePanel itself, then used that TreePanel's internal method for getting the it's Selection Model. After that you used that Selection Model's (in this case the DefaultSelectionModel) internal method to get the Selected Node.
You will find a wealth of information within the Ext JS Documentation. The online (and offline AIR app) API is quite extensive. The Ext Core manual can also give you a great deal of insight into ExtJS development, even if you aren't using the Core directly.
在 Ext JS 4 中,您可以在树面板上放置一个侦听器,如下所示:
In Ext JS 4 you can put a listener on the tree panel like this: