繁体   English   中英

如何在jstree中选择根节点?

[英]How do I select the root node in jstree?

我试图基于它是根节点还是子节点之一,在jstree中嵌入一些功能。 从我经过的jstree文档和其他Stack Overflow帖子中,我现在可以正确地填充项目,但是我遇到了如何在“ select_node”条件下选择jstree根节点的障碍,如下所示片段。 将单词“ root”附加到select_node上是否正确? 请指教。 (如果必须调用get_parent()并检查如何将根作为参数传递,则我的根项称为“根”)。 任何前进的指针将不胜感激。

$(".myjstree").on("select_node.jstree.root", function (e, data){
// do stuff if this is my root node
});

侦听select_node.jstree.root不能按预期方式工作,因为它所做的只是为select_node.jstree创建一个额外的名称空间(有关更多信息,请参阅http://api.jquery.com/on/上的“ 事件名称和名称空间”部分)。 )。 select_node.jstree.root事件仍然引发了所有select_node.jstree事件。

相反,您可以通过data.selected中的data.selected属性检查是否选择了select_node.jstree 您还可以通过$('#jstree').jstree(true).get_node('root')与之交互,如下所示:

 $('#jstree').jstree({ core: { data: [ { id: 'root', text: 'Root', state: { opened: true }, children: [ { id: 'child-node-1', text: 'Child 1' }, { id: 'child-node-2', text: 'Child 2' } ] } ] } }); $('#jstree').on("changed.jstree", function (e, data) { if (data.selected.length === 1 && data.selected[0] === 'root') { let root_node = $('#jstree').jstree(true).get_node('root'); console.log(root_node); } }); 
 @import "https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css"; 
 <div id="jstree"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM