简体   繁体   中英

How to get all parents node data when clicked/expanded in jstree

How to get all parents node data in jstree when i click a child node

suppose i have a jstree like below:

在此处输入图片说明

Observe: when i select File 2 i should get all parent node data i,e Root node 2 ---> Child 2 ---> File 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> 

Expected Output: (When i select node File 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"  }]

In the docs , you can see there is a changed.jstree event fired when you select a node. And you can use the get_selected and get_path methods to do what you want:

 // 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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM