jstree指定选中节点_jsTree:如何获取选定节点的ID到jsTree中的根节点?

How to get IDs of selected nodes to root node in jsTree?

Assume C is selected node then How can I get All parent IDs of C.

A

B

C

+C1

+c2

Following code will return only immediate parent ID:

If I selected C then I get only 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"))

});

Output:

Selected node = C

Parent of Selected node = B

Is there any way to get all parent nodes ID i.e. Selected node to root node ?

How to get all child nodes of selected node in jsTree ?

Any help or guidance in this matter would be appreciated.

解决方案

Use parents in jQuery to get all parents, filtering out by li because all tree items are li in jstree, try this:

var parents = data.rslt.obj.parents("li");

And for children use children in jQuery, like so:

var children = data.rslt.obj.parent().find('li');

EDIT Using the above, here's how to get all parent and children and put them in all an array for each:

Parents:

var parents = [];

data.rslt.obj.parents("li").each(function () {

parents.push({ id: $(this).attr("id"), description: $(this).children("a").text() });

});

Children:

var children = [];

data.rslt.obj.find("li").each(function () {

children.push({ id: $(this).attr("id"), description: $(this).children("a").text() });

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值