【发布时间】:2014-11-27 11:37:29
【问题描述】:
我正在获取子树名称,但我想获取其父节点名称的完整层次结构。
以下代码显示了我如何获取子节点并在特定的 div 元素中打印其值:
$(document).ready(function () {
$('#bdeViewNew').on('changed.jstree', function (e, data) {
var i, j, r = [];
for (i = 0, j = data.selected.length; i < j; i++) {
r.push(data.instance.get_node(data.selected[i]).text.trim());
$('#treeBreadCrumbs').html(r.join(', '));
}
});
});
现在它打印子节点的值,例如Child a。但是如果树结构如下所示,我想要如下所示:
Parent
Child 1
Child a
Child b
Child 2
Child c
Child d
所以如果我点击 Child a 我希望我的 div 内容更新为
Parent > Child 1 > Child a
到目前为止,我只收到Child a。请告诉我如何获得正确的输出。
我尝试如下所示获取所有父节点的路径:
$(document).ready(function() {
$('#bdeViewNew').on('changed.jstree', function(e, data) {
var ids = data.inst.get_path('#bdeViewNew' + data.rslt.obj.attr('id'),true);
var names = data.inst.get_path('#bdeViewNew' + data.rslt.obj.attr('id'),false);
alert("Path [ID or Name] from root node to selected node = ID's = "+ids+" :: Name's = "+names);
});
});
但 get_path 仍然没有结果。我需要使用不同的 JS 或插件吗? attr('id') 的含义是什么,我应该传递那个 li 的 id 或其他东西,因为我没有正确理解这个语法。
添加我的jstree:
<div id="bdeViewNew">
<ul>
<li id="bde" data-jstree='{"opened":true,"icon":"./images/tree.png"}'">
Parent 1
<ul>
<li id="aaa" data-jstree='{"opened":true,"icon":"./images/tree.png"}'>
Child 1 <c:forEach items="${empInfo.empList}"
var="empValue">
<ul>
<li id="bbb" data-jstree='{"icon":"./images/tree.png"}' >${empValue.empName}</li>
</ul>
</c:forEach>
</li>
</ul>
<ul>
<li id="ccc" data-jstree='{"icon":"./images/tree.png"}'>Child 2
<c:forEach items="${imgInfo.imgList}"
var="imgValue">
<ul>
<li id="ddd" data-jstree='{"icon":"./images/tree.png"}'>${imgValue.imgName}</li>
</ul>
</c:forEach>
</li>
</ul>
</li>
</ul>
</div>
【问题讨论】:
-
检查它的父母
-
@Manticore 我尝试使用一个函数来检查父节点,但它不工作如下: y=r.push(data.instance._get_parent(data.selected[i]).text);警报(y);如果您有任何其他有关功能的信息,请告诉我
标签: javascript jquery jstree