RTB
RTB

Reputation: 5833

How do I open all nodes in jquery Jstree?

I am using the following code:

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

With the following html:

<div id="treeview">
  <ul>
    <li>
      <a href="#">RTB</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');.

Upvotes: 46

Views: 65225

Answers (9)

RickL
RickL

Reputation: 3413

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

Upvotes: 0

arash
arash

Reputation: 211

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

Upvotes: 21

Fahad Alrashed
Fahad Alrashed

Reputation: 1502

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

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

Upvotes: 3

xilef
xilef

Reputation: 2397

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/

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

Upvotes: 2

Behnam
Behnam

Reputation: 6459

use simple code

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

Upvotes: 1

atmelino
atmelino

Reputation: 2997

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:

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

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

Upvotes: 36

albert yu
albert yu

Reputation: 165

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 version: 3.0.0-bata10

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

Upvotes: 10

BHAVIK GHODASARA
BHAVIK GHODASARA

Reputation: 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");
    }) 

Upvotes: -1

rodneyrehm
rodneyrehm

Reputation: 13557

The jsTree documentation is "sub optimal". The docs don't clearly state that the initialization works asynchronously. There's 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.

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

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

Upvotes: 64

Related Questions