簡體   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