jsTree中如何获取选中节点的所有ID到根节点?

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

jsTree中如何获取选中节点的ID到根节点?

假设F和D是选中的节点,我想得到所有的id包括A B C D F

以下代码将 return 仅立即选择 ID D 和 F

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

有没有办法获取所有父节点 ID,即选定节点到根节点?

调用get_path获取每个选定节点的路径。

类似于:

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