javascript 单击节点时打开分支?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4486032/
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-25 13:51:14  来源:igfitidea点击:

Open branch when clicking on a node?

javascriptjquerytreejstree

提问by Daniel

I'm stuck with jsTreehere. So far it works and i can browse and expand nodes with the [+] icon and open pages when clicking a node, BUT i still want it to expand all the immediate nodes whenever someone clicks on a node.

我在这里被jsTree困住了。到目前为止,它可以工作,我可以使用 [+] 图标浏览和展开节点,并在单击节点时打开页面,但我仍然希望它在有人单击节点时展开所有直接节点。

i had a look at around for at least 2 hours but couln't find anything. the official website is not very helpfull because they don't have enough examples, and its not very well documented. had a look at this one, but didn't work for me either: http://luban.danse.us/jazzclub/javascripts/jquery/jsTree/reference/_examples/2_operations.html

我环顾四周至少 2 小时,但什么也没找到。官方网站不是很有帮助,因为他们没有足够的例子,而且没有很好的记录。看看这个,但对我也不起作用:http: //luban.danse.us/jazzclub/javascripts/jquery/jsTree/reference/_examples/2_operations.html

i didn't even get an error message in firebug

我什至没有在 firebug 中收到错误消息

so here is how my code looks right now, tree init:

所以这是我的代码现在的样子,树初始化:

$(function () {
    $("#jstree").jstree({
    ....

function triggered by click on node

点击节点触发的功能

.delegate("a","click", function (e) { 
    //click on node
    var page_id = $(this).parent().attr("page_id");
    var idn = $(this).parent().attr("id").split("_")[1];
    /*
            dosnt seem to work either...
    $(this).jstree("openNode", $("#node_"+idn));
    $(this).jstree("openNode", "#node_"+idn);
    */
    page = "index.php?page_id="+page_id;
    //location.href = page;
})

.bind didnt work either:

.bind 也不起作用:

$(this).bind("open_node.jstree", function (event, data) { 
    if((data.inst._get_parent(data.rslt.obj)).length) { 
        data.inst._get_parent(data.rslt.obj).open_node(this, false); 
    } 
})

does anyone see what i'm missing here...?

有没有人看到我在这里错过了什么......?

回答by justind

You need to bind to select_node.jstree and call toggle_node on the tree instance when it's triggered:

您需要绑定到 select_node.jstree 并在触发时在树实例上调用 toggle_node :

For jsTree versions < 3.0:

对于 jsTree 版本 < 3.0:

$("#your_tree").bind("select_node.jstree", function(event, data) {
  // data.inst is the tree object, and data.rslt.obj is the node
  return data.inst.toggle_node(data.rslt.obj);
});

For jsTree versions >= 3.0

对于 jsTree 版本 >= 3.0

$("#your_tree").bind("select_node.jstree", function (e, data) {
    return data.instance.toggle_node(data.node);
});

回答by GDP

With a newer version of jsTree (3.0.0 according to jsTree.js), i had to change the code provided by @justind a bit to work:

使用较新版本的 jsTree(根据 jsTree.js 为 3.0.0),我不得不稍微更改@justind 提供的代码才能工作:

$("#jstree").bind("select_node.jstree", function (e, data) {
    return data.instance.toggle_node(data.node);
});

回答by SoulWanderer

I use this (casoUso is the page linked, fInvocaCasoUso is a function to make the call).

我使用这个(casoUso 是链接的页面,fInvocaCasoUso 是一个调用函数)。

  $("#demo1").bind("select_node.jstree", function (e, data)
                    {
                        if (data.rslt.obj.attr("casoUso")!=undefined)
                        {
                            fInvocaCasoUso(data.rslt.obj.attr("casoUso"));
                        }
                        else
                        {
                            $("#demo1").jstree("toggle_node",data.rslt.obj);
                        }
                    });

If the node has a link, it opens, if not, the sub-tree is opened. Anyway, you should be able to combine both sides of "if" to open the branch and execute your link. Maybe executing:

如果节点有链接,则打开,如果没有,则打开子树。无论如何,您应该能够结合“if”的两侧来打开分支并执行您的链接。也许执行:

       $("#demo1").jstree("toggle_node",data.rslt.obj);
       fInvocaCasoUso(data.rslt.obj.attr("casoUso"));

Would do it...

会不会...