How to get all the IDs of selected nodes to root node in jsTree?

1.1k Views Asked by At

How to get IDs of selected nodes to root node in jsTree?

Assume F and D are selected nodes , I want to get all the ids include A B C D F

  • A
    • B
      • C
        • D
        • E
      • F

Following code will return only immediate selected ids D and F

    var  getMenuIds = function(){
        var menuIds = $("#menu-tree").jstree("get_checked");
        window.alert(menuIds.join(","));
        $('#menuIds').val(menuIds.join(","));
     }

Is there any way to get all parent nodes ID i.e. Selected node to root node ?

1

There are 1 best solutions below

0
On BEST ANSWER

Call get_path to get the path to each selected node.

Something like:

var tree = $("#menu-tree");
var menuIds = tree.jstree("get_checked");
var paths = menuIds.map(function (id) { return tree.jstree("get_path", id); });

// remove duplicates
var selected = [];
var uniq = {};
paths.forEach(function (path) {
  path.forEach(function (id) {
    if (!uniq[id]) {
      uniq[id] = true;
      selected.push(id);
    }
  });
});

window.alert(selected.join(","));