单击节点时打开分支?

Dan*_*iel 26 javascript tree jquery jstree

我在这里遇到了jsTree.到目前为止,它可以工作,我可以使用[+]图标浏览和扩展节点,并在单击节点时打开页面,但我还是希望它在有人点击某个节点时展开所有直接节点.

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

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

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

$(function () {
    $("#jstree").jstree({
    ....
Run Code Online (Sandbox Code Playgroud)

单击节点触发的功能

.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;
})
Run Code Online (Sandbox Code Playgroud)

.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); 
    } 
})
Run Code Online (Sandbox Code Playgroud)

有谁看到我在这里失踪了......?

jus*_*ind 38

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

对于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);
});
Run Code Online (Sandbox Code Playgroud)

对于jsTree版本> = 3.0

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


GDP*_*GDP 5

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

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


Sou*_*rer 1

我使用这个(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);
                        }
                    });
Run Code Online (Sandbox Code Playgroud)

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

       $("#demo1").jstree("toggle_node",data.rslt.obj);
       fInvocaCasoUso(data.rslt.obj.attr("casoUso"));
Run Code Online (Sandbox Code Playgroud)

会做...