user5405873
user5405873

Reputation:

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:

enter image description here

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"  }]

Upvotes: 0

Views: 2499

Answers (1)

blex
blex

Reputation: 25634

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>

Upvotes: 1

Related Questions