JSTree检查选择的节点是叶或作出只剩可选可选、节点、只剩、JSTree

2023-09-10 20:20:17 作者:小污女

我创建了一个jstree如下:

I created a jstree as follows

$('#js-tree').jstree({
            'core' : {
              'data' : {
                'url' : "${pageContext.request.contextPath}/makeTree",
                "plugins" : [ "types", "search"],
                'data' : function (node) {
                  return { 'id' : node.id };
                }
              }
            }
            });

我要检查,如果选择的节点是在提交信息的叶节点 -

I want to check if a selected node is a leaf node while submitting the information -

var selectedLeaves = $("#js-tree").jstree("get_selected");

但是,这给了我ID的唯一的数组。我怎么要么使用is_leaf方法从所选节点筛选我们唯一的叶节点? 我提到的这个帖子 - Force用户只选择叶节点 该解决方案并没有为我工作。

But this gives me array of id's only. How do I either use is_leaf method to filter our only leaf nodes from the selected nodes? I referred to this post - Force user to select only leaf nodes This solution did not work for me.

推荐答案

我发现用一个参数 get_selected API来过滤掉叶子我自己的解决方案文件夹 -

I found a solution on my own using an argument to the get_selected api to filter out the leaves from the folders -

var nodes = $("#js-tree").jstree(true).get_selected("full", true);
$.each(nodes,function(i, node){
  if(node.children.length >0){
    console.log("not leaf");
  }
  else{
    console.log("leaf");
  }  
});

如果你有一个更好的解决方案,请让我知道。

If you have a better solution please let me know.

 
精彩推荐
图片推荐