gpt4 book ai didi

javascript - 如何在jstree中单击/展开时获取所有父节点数据

转载 作者:行者123 更新时间:2023-12-03 00:28:24 27 4
gpt4 key购买 nike

当我点击子节点时如何获取jstree中所有父节点数据

假设我有一个jstree,如下所示:

enter image description here

观察:当我选择文件2时,我应该获取所有父节点数据,即根节点2 --->子节点2 --->文件2

$('#using_json_2').jstree({ 'core' : {
'data' : [
{ "id" : "ajson1", "parent" : "#", "text" : "Simple root node", "date":"2018"},
{ "id" : "ajson2", "parent" : "#", "text" : "Root node 2", "date":"2018"},
{ "id" : "ajson3", "parent" : "ajson2", "text" : "Child 1", "date":"12" },
{ "id" : "ajson4", "parent" : "ajson2", "text" : "Child 2", "date":"12" },
{ "id" : "ajson5", "parent" : "ajson4", "text" : "File 1", "date":"12","children": false,"icon":"fa fa-file-o" },
{ "id" : "ajson6", "parent" : "ajson4", "text" : "File 2", "date":"12","children": false,"icon":"fa fa-file-o" }
]
} });
<link href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" rel="stylesheet"/>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>


<div id="using_json_2"></div>

预期输出:(当我选择节点文件2时)

var allParentsNode = [
{ "id" : "ajson2", "parent" : "#", "text" : "Root node 2"},
{ "id" : "ajson4", "parent" : "ajson2", "text" : "Child 2" },
{ "id" : "ajson6", "parent" : "ajson4", "text" : "File 2", "children": false,"icon":"fa fa-file-o" }]

最佳答案

docs ,您可以看到选择节点时会触发 changed.jstree 事件。您可以使用 get_selectedget_path 方法来执行您想要的操作:

// Make it a variable so you can access it later
var treeData = [
{ "id" : "ajson1", "parent" : "#", "text" : "Simple root node", "date":"2018"},
{ "id" : "ajson2", "parent" : "#", "text" : "Root node 2", "date":"2018"},
{ "id" : "ajson3", "parent" : "ajson2", "text" : "Child 1", "date":"12" },
{ "id" : "ajson4", "parent" : "ajson2", "text" : "Child 2", "date":"12" },
{ "id" : "ajson5", "parent" : "ajson4", "text" : "File 1", "date":"12","children": false,"icon":"fa fa-file-o" },
{ "id" : "ajson6", "parent" : "ajson4", "text" : "File 2", "date":"12","children": false,"icon":"fa fa-file-o" }
];

var myTree = $('#using_json_2').jstree({ 'core' : {
'data' : treeData // Use it here
}});

myTree.on('changed.jstree', function(e, data) {
var selected = data.instance.get_selected(),
// Get the path (array of IDs, since we pass true)
path = data.instance.get_path(selected, null, true),
// Use `map` to retrieve all nodes
node_path = path.map(function(id) {
return treeData.find(function(node) { return node.id === id; });
});
console.log(node_path);
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>

<div id="using_json_2"></div>

关于javascript - 如何在jstree中单击/展开时获取所有父节点数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53979387/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com