Javascript 如何在 jquery Jstree 中打开所有节点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11018242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:02:38  来源:igfitidea点击:

How do i open all nodes in jquery Jstree?

javascriptjqueryjstree

提问by RTB

i'm using the following code:

我正在使用以下代码:

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

With the following html:

使用以下 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>

My problem is that all nodes stay closed, i can't get them to open with jstree('open_all');

我的问题是所有节点都保持关闭状态,我无法使用 jstree('open_all'); 打开它们。

回答by rodneyrehm

The jsTree documentationis "sub optimal". The docs don't clearly state that the initialization works asynchronously. There's core.loaded():

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

A dummy function, whose purpose is only to trigger the loaded event. This event is triggered once after the tree's root nodes are loaded, but before any nodes set in initially_open are opened.

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

This suggests an event loaded.jstreeis fired after the tree is setup. You can hook into that event to open all your nodes:

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

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

回答by atmelino

I am using version 3 of jstree and Chrome. The loaded event did not work for me, but the ready event did, even after the jstree instance was created:

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

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

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

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

回答by arash

If you want open all node when tree loaded:

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

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

回答by albert yu

all the answers above not work in my workspace. I searched again and find this link(Why doesn't jsTree open_all() work for me?) is helpful, and post my answer:

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

jstree version: 3.0.0-bata10

jstree 版本:3.0.0-bata10

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

回答by Behnam Mohammadi

use simple code

使用简单的代码

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

回答by xilef

When using html data 'you can set the jstree-open class on any <li> element to make it initially extended, so that its children are visible.' - https://www.jstree.com/docs/html/

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

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

回答by Fahad Alrashed

I tried all the answers here and they didn't work with jsTree (v3.3.4). What worked is the load_node.jstreeevent:

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

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

回答by RickL

You can also apply animation to the opening and closing like so:

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

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

(or similarly apply to .on('ready.jstree', function() { // code here });

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

回答by BHAVIK GHODASARA

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