2012-06-13 48 views
32

我使用下面的代码:我如何打开jQuery Jstree中的所有节点?

$("#treeview").jstree(); 
$("#treeview").jstree('open_all'); 

用下面的HTML:

<div id="treeview"> 
    <ul> 
    <li> 
     <a href="#">rubentebogt</a> 
     <ul> 
     <li> 
      <a href="#" onclick="goTo('index.php?module=alarm&amp;pagina=dashboard&amp;id=6',false);">Beneden</a> 
     </li> 
     <li> 
      <a href="#" onclick="goTo('index.php?module=alarm&amp;pagina=dashboard&amp;id=7',false);">Boven</a> 
     </li> 
     </ul> 
    </li> 
    </ul> 
</div> 

我的问题是,留关闭所有节点,我不能让他们与jstree打开(” open_all');

+0

而问题是? – Andreas

+0

你可以发布你生成的HTML而不是Smarty吗? –

+1

我编辑了我的问题,谢谢你们。 – RTB

回答

47

jsTree documentation是“次优”。文档没有明确说明初始化是异步工作的。有core.loaded()

一个虚拟函数,其目的只是为了触发加载的事件。此事件在树的根节点加载后触发一次,但是在打开在starts_open中设置的任何节点之前触发。

这表明事件loaded.jstree在树设置后触发。你可以连接到该事件,打开所有的节点:

var $treeview = $("#treeview"); 
$treeview 
    .jstree(options) 
    .on('loaded.jstree', function() { 
    $treeview.jstree('open_all'); 
    }); 
+0

这也适用于refresh.jstree事件吗? – RTB

+1

使用此代码后,我遇到了相反的问题。我最终在“on”调用之后再次调用jstree('open_all'),因为chrome在事件注册之前完成了树 –

+0

此答案仍然有效吗?我似乎无法让它为我的树工作,尽管它看起来非常简单。 – Smoore

15

如果你想打开的所有节点,当树装:以上

$("#treeview") 
    // call `.jstree` with the options object 
    .jstree({ 
     "plugins" : ["themes", "html_data","ui","crrm","sort"] 
    }) 
    .bind("loaded.jstree", function (event, data) { 
     // you get two params - event & data - check the core docs for a detailed description 
     $(this).jstree("open_all"); 
    })  
}); 
+4

对于原始海报的缘故,它有助于给出更多解释为什么这将起作用 - 即在这种情况下解释了'.jstree()'异步运行并在事件准备好时触发事件。 –

+1

感谢代码,它适用于我,顺便说一句,你有一个额外的})的代码。 –

-1
.bind("loaded.jstree", function (event, data) { 
     // you get two params - event & data - check the core docs for a detailed description 
     $(this).jstree("open_all"); 
    }) 
7

所有的答案在我的工作区无法正常工作。 我再次搜索,找到这个链接(Why doesn't jsTree open_all() work for me?)是有帮助的,并张贴我的答案:

jstree版本:3.0.0-bata10

$(document).ready(function() { 
    $("#tree").bind("loaded.jstree", function(event, data) { 
    data.instance.open_all(); 
    }); 
    $("#tree").jstree(); 
}) 
+0

简单地将ready.jstree绑定为atmelino提到并解决了问题 – mikus

1

使用简单的代码

$(".jstree").jstree().on('loaded.jstree', function() { 
    $(".jstree").jstree('open_all'); 
}) 
0

当使用HTML数据“你可以在任何<li>元素上设置jstree-open类,以使其初始扩展,以便其子元素可见。 - https://www.jstree.com/docs/html/

<li class="jstree-open" id="node_1">My Open Node</li> 
0

我在这里尝试了所有的答案,他们没有jsTree(v3.3.4)的工作。什么工作是load_node.jstree事件:

.on('load_node.jstree', function() { 
     jstree.jstree("open_all"); 
    })