[英]jsTree : How to get IDs of selected nodes to root node in jsTree?
如何在jsTree中將所選節點的ID獲取到根節點?
假設C是選定節點然后我如何獲得C的所有父ID。
一種
乙
C
+ C1
+ C2
以下代碼將僅返回直接父ID:如果我選擇了C,那么我只獲得B.
.bind("select_node.jstree", function (event, data) {
//`data.rslt.obj` is the jquery extended node that was clicked
alert("Selected node = "+ data.rslt.obj.attr("id"));
alert("Parent of Selected node = "+ data.inst._get_parent(data.rslt.obj).attr("id"))
});
輸出:
Selected node = C
Parent of Selected node = B
有沒有辦法獲取所有父節點ID,即選擇節點到根節點?
任何有關此事的幫助或指導將不勝感激。
在jQuery中使用parents
來獲取所有父項,由li
過濾掉,因為所有樹項都是jstree
中的li
,試試這個:
var parents = data.rslt.obj.parents("li");
對於孩子children
在jQuery中使用children
,就像這樣:
var children = data.rslt.obj.parent().find('li');
編輯使用上面的內容,這里是如何獲取所有父級和子級,並將它們放在每個的所有數組中:
父母:
var parents = [];
data.rslt.obj.parents("li").each(function () {
parents.push({ id: $(this).attr("id"), description: $(this).children("a").text() });
});
兒童:
var children = [];
data.rslt.obj.find("li").each(function () {
children.push({ id: $(this).attr("id"), description: $(this).children("a").text() });
});
1更簡單的解決方案
.get_path ( node , id_mode )
將路徑返回到節點,可以是ID數組,也可以是節點名數組。 mixed node:這可以是指向樹中元素的DOM節點,jQuery節點或選擇器,我們想要它的路徑.bool id_mode:如果設置為true,則返回ID而不是父節點的名稱。 默認值為false。
// To get path [ID or Name] from root node to selected node
var ids = data.inst.get_path('#' + data.rslt.obj.attr('id'),true);
// Returns IDs from root to selected node
var names = data.inst.get_path('#' + data.rslt.obj.attr('id'),false);
// Returns Name's from root to selected node
alert("Path [ID or Name] from root node to selected node = ID's = "+ids+" :: Name's = "+names);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.