26

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

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

我什至没有在萤火虫中收到错误消息

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

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

点击节点触发的函数

.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 也不起作用:

$(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); 
    } 
})

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

4

3 回答 3

38

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

对于 < 3.0 的 jsTree 版本:

$("#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);
});

对于 jsTree 版本 >= 3.0

$("#your_tree").bind("select_node.jstree", function (e, data) {
    return data.instance.toggle_node(data.node);
});
于 2011-10-14T19:04:53.287 回答
5

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

$("#jstree").bind("select_node.jstree", function (e, data) {
    return data.instance.toggle_node(data.node);
});
于 2014-02-01T14:32:47.507 回答
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);
                        }
                    });

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

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

会做...

于 2010-12-28T09:10:56.023 回答