jstree获取所选节点的级别 [英] jstree get the level of selected node
本文介绍了jstree获取所选节点的级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获得当前所选节点的学位/级别?即它拥有的父母数量。
How can I get the degree/level of the currently selected node? I.e. the number of parents it has.
我有一个部分在选择节点时执行:
I have the section which executes on selection of a node as such:
.bind("select_node.jstree", function (event, data) {
var nodeInfo = $("#" + data.rslt.obj.attr("id"));
console.log(data.rslt.obj.attr("id")); // the id of the node
console.log(nodeInfo.children("a").text()); // the name of the node
// the level of the node???
});
推荐答案
data.inst.get_path ()。length
1是根节点;
.bind("select_node.jstree",function(e,data) {
var inst=data.inst;
var level=inst.get_path().length;
var selected=inst.get_selected();
var id=selected.attr('id');
var name=selected.prop('tagName');
console.log(name,id,level);
});
(最好使用inst,而不是用dom搜索);
(better to use inst, instead of searching with dom);
$(function () {
$("#demo1").jstree({
"plugins" : [ "themes", "html_data", "ui", "cookies" ]
});
$("#demo2").jstree({
"plugins" : [ "themes", "html_data", "ui", "cookies" ]
});
$('#demo1,#demo2').bind("select_node.jstree",function(e,data) {
var inst=data.inst;
var level=inst.get_path().length;
var selected=inst.get_selected();
var id=selected.attr('id');
var name=selected.prop('tagName');
console.log(name,id,level);
});
});
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" />
<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>
With id-s:
<div id="demo1" class="demo">
<ul>
<li id="phtml_1">
<a href="#">Root node 1</a>
<ul>
<li id="phtml_2">
<a href="#">Child node 1</a>
</li>
<li id="phtml_3">
<a href="#">Child node 2</a>
</li>
</ul>
</li>
<li id="phtml_4">
<a href="#">Root node 2</a>
</li>
</ul>
</div>
Without id-s:
<div id="demo2" class="demo">
<ul>
<li>
<a href="#">Root node 1</a>
<ul>
<li>
<a href="#">Child node 1</a>
</li>
<li>
<a href="#">Child node 2</a>
</li>
</ul>
</li>
<li>
<a href="#">Root node 2</a>
</li>
</ul>
</div>
这篇关于jstree获取所选节点的级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文