【问题标题】:Open branch when clicking on a node?单击节点时打开分支?
【发布时间】:2010-12-20 00:40:05
【问题描述】:

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

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

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

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

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

有人看到我在这里缺少什么吗...?

【问题讨论】:

    标签: javascript jquery tree jstree


    【解决方案1】:

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

    对于 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);
    });
    

    【讨论】:

    • 工作,谢谢!但是 >= 3 中的参数不同。见:jstree.com/api/#/?q=select_node.jstree&f=select_node.jstree 现在是function (node, selected, event)
    • 尼克,您能否扩展您使用的功能的更新版本,以便我可以更新答案?在您使用新参数进行更改时,新的 toggle_node 调用是什么? data.instance.toggle_node(node); ?
    【解决方案2】:

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

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

    【讨论】:

    • 我更愿意坚持使用 API 使用 $(e.currentTarget).jstree('toggle_node', data.node) 而不是像 .instance 这样的内部事物
    【解决方案3】:

    我用这个(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"));
    

    会不会...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-30
      • 2011-06-07
      • 2023-01-13
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      相关资源
      最近更新 更多