order the selected (and undetermined) nodes

267 views
Skip to first unread message

Gopal Peddinti

unread,
Jul 2, 2020, 5:30:30 AM7/2/20
to jsTree
Hi,

I need to export selected nodes (and their undetermined parents). I am able to get the selected and undetermined nodes as needed, but I want to get these nodes in the tree order. Here is my current code.

$('#exportbutton').on('click', function() {
   
// get the ids of the selected nodes. // false causes only the ID to be returned
   
var selectedNodes = $('#myjstree').jstree('get_selected', false);

   
var selectedAndUndetermined = [];
   
for(i = 0; i < selectedNodes.length; i++) {
     
// collect the ids of the selected nodes along with their parents
      selectedAndUndetermined
= selectedAndUndetermined.concat($('#myjstree').jstree('get_path', selectedNodes[i], false, true));
   
}
   
// remove duplicates (i.e. create an array with unique elements)
    selectedAndUndetermined
= Array.from(new Set(selectedAndUndetermined));

   
// create the selected subtree (the combination of selected and undetermined nodes)
   
var subtree = [];
   
for(j = 0; j < selectedAndUndetermined.length; j++) {
       nodei
= $('#myjstree').jstree('get_node', selectedAndUndetermined[j], false);
       subtree
= subtree.concat(nodei);
   
}

    console
.log(subtree);



Could you please advise how to get nodes in `subtree`  in the same order in which they appear in the tree?

Thank you very much.

Best regards,
Gopal

Gopal Peddinti

unread,
Jul 3, 2020, 5:03:12 AM7/3/20
to jsTree

In my case, however, the json data is in the (id, parent) format (i.e. no node.children element).

Gopal Peddinti

unread,
Jul 3, 2020, 3:04:53 PM7/3/20
to jsTree
In my case, however, the json data is in the (id, parent) format (i.e. no node.children element).

Sorry, this was just my misunderstanding apparently. I got the correct order with the logic given in the post I referred to (https://groups.google.com/forum/#!searchin/jstree/order$20selected$20nodes%7Csort:date/jstree/oeaADRK5j50/ln6cHF415eYJ). My code is below:

$('#exportbutton').on('click', function() {
    // get the ids of the selected nodes. // false causes only the ID to be returned
    var selectedNodes = $('#myjstree').jstree('get_selected', false);
    var undeterminedNodes = $('#myjstree').jstree('get_undetermined', false);
    var selectedAndUndetermined = selectedNodes.concat(undeterminedNodes);
    console.log('selected and undetermined', selectedAndUndetermined);

    var jsonFullTree = $('#myjstree').jstree('get_json', '#', { 'flat' : true });

    const subTree = jsonFullTree.filter((mynode) => selectedAndUndetermined.includes(mynode.id));
    console.log('subtree', subTree);


Reply all
Reply to author
Forward
0 new messages