43

我正在使用以下代码:

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

4

9 回答 9

61

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

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

loaded.jstree这表明在设置树后会触发一个事件。您可以挂钩该事件以打开所有节点:

var $treeview = $("#treeview");
$treeview
  .jstree(options)
  .on('loaded.jstree', function() {
    $treeview.jstree('open_all');
  });
于 2012-06-13T16:02:06.973 回答
34

我正在使用 jstree 和 Chrome 的第 3 版。load 事件对我不起作用,但 ready 事件对我起作用,即使在创建 jstree 实例之后:

$('#treeview').on('ready.jstree', function() {
    $("#treeview").jstree("open_all");          
});

http://www.jstree.com/api/#/?q=.jstree%20Event&f=ready.jstree

于 2014-05-19T06:21:39.613 回答
20

如果要在加载树时打开所有节点:

$("#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");
    })      
});
于 2012-09-23T16:22:50.940 回答
10

以上所有答案都不适用于我的工作区。我再次搜索并找到此链接(为什么 jsTree open_all() 不适合我?)很有帮助,并发布我的答案:

jstree版本:3.0.0-bata10

$(document).ready(function() {
  $("#tree").bind("loaded.jstree", function(event, data) { 
    data.instance.open_all();
  });
  $("#tree").jstree();
})
于 2014-03-28T07:33:33.213 回答
1

使用简单的代码

$(".jstree").jstree().on('loaded.jstree', function () {
     $(".jstree").jstree('open_all');
})
于 2014-07-07T11:43:46.893 回答
1

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

<li class="jstree-open" id="node_1">My Open Node</li>
于 2016-03-03T06:56:06.467 回答
0

我在这里尝试了所有答案,但它们不适用于 jsTree (v3.3.4)。有效的是load_node.jstree事件:

    .on( 'load_node.jstree', function () {
      jstree.jstree( "open_all" );
    } )
于 2017-12-30T08:17:11.593 回答
0

您还可以将动画应用于打开和关闭,如下所示:

$(document)
    .on("click", "#open-all-folders", function () {
        // param1 set to null to open/close all nodes
        // param2 is the duration in milliseconds
        $("#tree-ref").jstree().open_all(null, 200);
    })
    .on("click", "#close-all-folders", function () {
        $("#tree-ref").jstree().close_all(null, 200);
    });

(或类似地适用于.on('ready.jstree', function() { // code here });

于 2018-08-06T12:02:33.523 回答
-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");
    }) 
于 2013-10-25T08:46:54.133 回答