javascript 如何将节点标识为根节点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10069548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:35:33  来源:igfitidea点击:

How do I identify a node as being a root node?

javascriptjqueryjquery-pluginsjstree

提问by Eddy Pronk

I want to create a node inside or under an existing node depending on if it is a root node. (a tree widget is usually a list of trees or a tree without a visible root node.)

我想在现有节点内部或下方创建一个节点,具体取决于它是否是根节点。(树小部件通常是树的列表或没有可见根节点的树。)

I tried get_parent, but how do I know if that is a root node?

我试过 get_parent,但我怎么知道那是不是根节点?

var parent = $("#demo1").jstree('_get_parent', $("#foo"));
var node = $("#demo1").jstree('_get_node', $("#foo"));

What confused me is that get_node seems to return the same object as get_parent.

让我感到困惑的是 get_node 似乎返回与 get_parent 相同的对象。

I'm using jstree_pre1.0_fix_1.

我正在使用 jstree_pre1.0_fix_1。

edited:

编辑:

I ended up checking for the known id of the parent of the parent.

我最终检查了父级的父级的已知 ID。

var node = $(e.replyto);
if (node.length) {
  if (node.parent().parent().attr('id') == 'demo1') {
    $("#demo1").jstree("create_node", node, 'last',{'attr': {'id':e.id}, 'state':'open', 'data': e.data}) ;
  } else {
    $("#demo1").jstree("create_node", node, 'after',{'attr': {'id':e.id}, 'state':'open', 'data': e.data}) ;
  }
} else {
    $("#demo1").jstree("create_node", -1, 'after',{'attr': {'id':e.id}, 'state':'open', 'data': e.data});
}

回答by Nada_Surf

You can call get_parent()on the node. If it returns '#' then the node is a root node. E.G:

您可以在节点上调用get_parent()。如果它返回“#”,则该节点是根节点。例如:

var node = ...;

if($('#demo1').jstree(true).get_parent(node) == '#') {
    // node is a root node
}

回答by Thomas Guillory

It's not the ideal solution but you can use _get_childrenwith -1in param to get all root nodes and test if your node is in the list.

这不是理想的解决方案,但您可以使用_get_childrenwith -1in param 来获取所有根节点并测试您的节点是否在列表中。

._get_children ( node )
  Use -1 to return all root nodes.

(from http://www.jstree.com/documentation/core)

(来自http://www.jstree.com/documentation/core

回答by DavidHyogo

I've been struggling a bit with this because I'm trying to use the context menu plugin. I don't want the user to be able to create a new root node. I only want to allow them to create sub-nodes. (The root nodes represent the groups that the current user belongs to so is pre-set by the administrator). My first confusion was that _get_childrenreturns an object with a lengthproperty. It's not an array but it has a lengthproperty corresponding correctly to the number of actual root nodes. Looking at the underlying code, the jstree_get_childrenmethod uses jQuery's childrenmethod, which returns the child nodes with indexes 0, 1, 2 etc along with other jQuery properties and methods. I found it convenient to extract just the array of nodes and use indexOfto check if the current node is a root node. So, here is a snippet from the itemsproperty of my jstreecontext menu configuration:

我一直在为此苦苦挣扎,因为我正在尝试使用上下文菜单插件。我不希望用户能够创建新的根节点。我只想让他们创建子节点。(根节点代表当前用户所属的组,由管理员预先设置)。我的第一个困惑是_get_children返回一个带有length属性的对象。它不是一个数组,但它有一个length与实际根节点数正确对应的属性。查看底层代码,该jstree_get_children方法使用 jQuery 的children方法,该方法返回索引为 0、1、2 等的子节点以及其他 jQuery 属性和方法。我发现仅提取节点数组并使用很方便indexOf检查当前节点是否为根节点。所以,这是items我的jstree上下文菜单配置的属性的一个片段:

'items': function(node){
    var rootChildren = this._get_children(-1), 
    rootNodes = [], 
    i;
    //rootChildren is now a fancy jQuery object with the child nodes assigned
    //to keys 0, 1 etc.
    //Now create a simple array
    for(i = 0; i < rootChildren.length; i += 1){
        rootNodes[i] = rootChildren[i];
    }
    //We can now use indexOf to check if the current node is in that array
    //Note again that node is a fancy jQuery object, the actual DOM element
    //is stored in node[0]
    console.log(rootNodes.indexOf(node[0]) > -1);
    //code here to add whatever items you want to the context menu.
}

If you right click around your tree, you will see truein the console window for the root nodes and falsefor any nodes lower down the hierarchy. Note that for IE below 8 (I think), you will need to supply an indexOfmethod for Arraybecause earlier versions of IE don't supply indexOfas a native method.

如果您在树上右键单击,您将true在控制台窗口中看到根节点和false层次结构下方的任何节点。请注意,对于低于 8 的 IE(我认为),您需要提供一种indexOf方法,Array因为早期版本的 IE 不indexOf作为本机方法提供。