Jeff Dege
Jeff Dege

Reputation: 11700

Delete all nodes in jsTree

Is there a way to clear out all nodes from a jsTree that's faster than walking through all the nodes deleting them one-by-one?

Upvotes: 16

Views: 24236

Answers (7)

Bruno Lafont
Bruno Lafont

Reputation: 31

myTree.delete_node(myTree.get_node("#").children);

Upvotes: 2

Ravi Khambhati
Ravi Khambhati

Reputation: 743

$('#jstree').parent().html('<div id="#jstree"></div>');

Upvotes: 0

user602599
user602599

Reputation: 661

$('#tree').jstree("destroy").empty();

This is what worked for me. First destroy jstree elements and associated events, and then empty the div containing jstree.

Upvotes: 13

dnshio
dnshio

Reputation: 924

The following call will destroy the current instance of jsTree, remove any bound event listeners and obviously achieve your ultimate goal of removing all nodes. But this method is a bit of a over-kill, it has to be said.

$("#DivElementContainingYourTree").jstree("init");

Upvotes: 0

Brad
Brad

Reputation: 163234

The simplest way I have found is to simply call .empty on the div containing the tree.

$('#tree').empty();

You might choose to use a more specific selector as a parameter for empty(), but this works fine for me.

Upvotes: 13

Adam Terlson
Adam Terlson

Reputation: 12730

See the documentation here: http://www.jstree.com/documentation/core

.delete_node ( node )

Removes a node. Triggers an event.

mixed node

This can be a DOM node, jQuery node or selector pointing to the element you want to remove.

It seems you can just do a selector that will delete all the nodes you want, no loops required.

Upvotes: 11

mikerobi
mikerobi

Reputation: 20878

Call .remove(node) on the root nodes.

Upvotes: 1

Related Questions