简体   繁体   中英

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

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 ABCDF

  • 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 ie Selected node to root node ?

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(","));

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