在我的页面上,我有 2 棵树。 页面上有表格。一棵树显示在页面上,另一棵树显示在表单内。 我创建了如下树:
var myTree1 = Ext.create('Ext.tree.Panel', { store: store, //where store is hardcoded json tree data .... });
类似地,myTree2 被声明为不同的商店。它显示在一个表单中。 当我选择 myTree2 上的任何节点并单击创建按钮时,我必须能够在 myTree1 中的同一索引处添加一个新的叶节点。
我需要的帮助是:
1. 如何获取选中节点的索引。
2.如何去到myTree1中的索引,应该等于选择的索引myTree2。
3. 如何在特定索引处添加叶节点。
最佳答案
供引用:
Ext JS 4 TreePanel documentation
Ext JS 4 NodeInterface documentation
<select
事件同时提供索引和记录。如果您需要对其进行一般访问,myTree1.getSelectionModel().getSelection()
将返回一个记录数组,然后您可以对照存储获取索引。我假设您想比较两棵树上的选定记录。给定 #1 中的示例,试试这个:
var tree1rec = myTree1.getSelectionModel().getSelection[0], tree2rec = myTree2.getSelectionModel().getSelection[0]; // Perform comparison logic here // Note that if you're using two different stores, you can't // just do an equality test. You'll probably have to compare // individual store values.
节点是相对于树上的现有节点插入的。所以如果你有一个按钮点击事件:
function onButtonClick(button, eOpts) { var rec = myTree1.getSelectionModel().getSelection[0]; if (rec) { var childNode = rec.createNode(/* some object here */); rec.appendChild(childNode); } }
细节会根据您的实际数据而有所不同,但这是一般模式。
关于javascript - 我们如何获取 extjs 树中选择节点的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9614846/