What is jsTree?
jsTree is jquery plugin, that provides interactive trees. It is absolutely free, open source and distributed under the MIT license. jsTree is easily extendable, themable and configurable, it supports HTML & JSON data sources and AJAX loading.
jsTree functions properly in either box-model (content-box or border-box), can be loaded as an AMD module, and has a built in mobile theme for responsive design, that can easily be customized. It uses jQuery's event system, so binding callbacks on various events in the tree is familiar and easy.
Just a few of the features worth noting:
- drag & drop support
- keyboard navigation
- inline edit, create and delete
- tri-state checkboxes
- fuzzy searching
- customizable node types
All modern browsers are supported, as well as IE8
Download
Discuss
Report bugs
Donate
Getting Started - everything at a glance
-
Download jsTree
All the files you need are in the
dist/
folder of the download. -
Include a jsTree theme
Themes can be autloaded too, but it is best for performance to include the CSS file.
<link rel="stylesheet" href="dist/themes/default/style.min.css" />
-
Setup a container
This is the element where you want the tree to appear, a
<div>
is enough. This example has a nested<ul>
as there is no other data source configured (such as JSON).<div id="jstree_demo_div"></div>
-
Include jQuery
jsTree requires 1.9.0 or greater in your webpage.
<script src="dist/libs/jquery.js"></script>
-
Include jsTree
For production include the minified version:
dist/jstree.min.js
, there is a development version too:dist/jstree.js
<script src="dist/jstree.min.js"></script>
-
Create an instance
Once the DOM is ready you can start creating jstree instances.
$(function () { $('#jstree_demo_div').jstree(); });
-
Listen for events
jsTree uses events to notify you when something changes while users (or you) interact with the tree. So binding to jstree events is as easy binding to a click. There is a list of events and what information they provide in the API documentation.
$('#jstree_demo_div').on("changed.jstree", function (e, data) { console.log(data.selected); });
-
Interact with your instances
Once an instance is ready you can invoke methods on it. There is a list of available methods in the API documentation. The three examples below do exactly the same thing
$('button').on('click', function () { $('#jstree').jstree(true).select_node('child_node_1'); $('#jstree').jstree('select_node', 'child_node_1'); $.jstree.reference('#jstree').select_node('child_node_1'); });
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jsTree test</title>
<!-- 2 load the theme CSS file -->
<link rel="stylesheet" href="dist/themes/default/style.min.css" />
</head>
<body>
<!-- 3 setup a container element -->
<div id="jstree">
<!-- in this example the tree is populated from inline HTML -->
<ul>
<li>Root node 1
<ul>
<li id="child_node_1">Child node 1</li>
<li>Child node 2</li>
</ul>
</li>
<li>Root node 2</li>
</ul>
</div>
<button>demo button</button>
<!-- 4 include the jQuery library -->
<script src="dist/libs/jquery.js"></script>
<!-- 5 include the minified jstree source -->
<script src="dist/jstree.min.js"></script>
<script>
$(function () {
// 6 create an instance when the DOM is ready
$('#jstree').jstree();
// 7 bind to events triggered on the tree
$('#jstree').on("changed.jstree", function (e, data) {
console.log(data.selected);
});
// 8 interact with the tree - either way is OK
$('button').on('click', function () {
$('#jstree').jstree(true).select_node('child_node_1');
$('#jstree').jstree('select_node', 'child_node_1');
$.jstree.reference('#jstree').select_node('child_node_1');
});
});
</script>
</body>
</html>
Configuring instances
Creating an instance as described in the overview does not modify any of the defaults:
$('#jstree').jstree();
You can change the defaults for all future instances:
$.jstree.defaults.core.theme.variant = "large";
$('#jstree').jstree();
But most of the time you will want to change the defaults only for the instance that is being created. This is achieved by passing in a config object when creating the instance:
$('#jstree').jstree({
"plugins" : [ "wholerow", "checkbox" ]
});
As seen in the previous example - there is one special key in the config object named plugins
. It is an array of strings, which contain the names of the plugins you want active on that instance.
All options that do not depend on a plugin are contained in a key of the config object named core
, the options for each plugin are contained within a key with the same name as the plugin:
$('#jstree').jstree({
"core" : {
"theme" : {
"variant" : "large"
}
},
"checkbox" : {
"keep_selected_style" : false
},
"plugins" : [ "wholerow", "checkbox" ]
});
You can have a look at all the options and their default values. This list is what you can configure on each instance.
For example, by default the tree allows multiple selection as stated in $.jstree.defaults.core.multiple
, to overwrite that make sure your config object contains "core" : { "multiple" : false }
. If you have multiple overrides for the same key (like "core"
here), group them:
$("#jstree").jstree({
"core" : {
"multiple" : false,
"animation" : 0
}
});
Populating a tree using HTML
Basic markup
jsTree can turn a regular unordered list into a tree. The minimal required markup is a <ul>
node with some nested <li>
nodes with some text inside.
You should have a container wrapping the <ul>
and create the instance on that container. Like so:$('#html1').jstree();
.
<div id="html1">
<ul>
<li>Root node 1</li>
<li>Root node 2</li>
</ul>
</div>
Nodes with children
To create a node with child nodes simpy nest an <ul>
.
Internally jstree converts the text to a link, so if there already is a link in the markup jstree won't mind. Like Child node 2
.
Clicking on the link however will not direct the user to a new page, to do that - intercept the changed.jstree
event and act accordingly.
Keep reading for the section on handling events.
<div id="html1">
<ul>
<li>Root node 1
<ul>
<li>Child node 1</li>
<li><a href="#">Child node 2</a></li>
</ul>
</li>
</ul>
</div>
Setting initial state with classes
To make a node initially selected you can set the jstree-clicked
class on the <a>
element.
Similarly you can set the jstree-open
class on any <li>
element to make it initially extended, so that its children are visible.
It is a good idea to give your nodes unique IDs by adding the id
attribute to any <li>
element. This will be useful if you need to sync with a backend as you will get the ID back in any events jstree triggers.
…
<li class="jstree-open" id="node_1">Root</li>
<ul>
<li>
<a href="#" class="jstree-clicked">Child</a>
</li>
</ul>
</li>
…
Setting initial state with data attribute
You can also set the state on a node using a data-jstree
attribute.
You can use any combination of the following: opened
, selected
, disabled
, icon
.
Specifying an address (anything containing a /
) will display that image as the node icon. Using a string will apply that class to the <i>
element that is used to represent the icon.
For example if you are using Twitter Bootstrap you can use "icon" : "glyphicon glyphicon-leaf"
to display a leaf icon.
<li data-jstree='{"opened":true,"selected":true}'>Root
<ul>
<li data-jstree='{"disabled":true}'>Child</li>
<li data-jstree='{"icon":"http://jstree.com/tree.png"}'>
Child</li>
<li data-jstree='{"icon":"glyphicon glyphicon-leaf"}'>
Child</li>
</ul>
</li>
Loading with AJAX
You can also use AJAX to populate the tree with HTML your server returns. The format remains the same as the above, the only difference is that the HTML is not inside the container, but returned from the server.
To take advantage of this option you need to use the $.jstree.defaults.core.data
config option.
Just use a standard jQuery-like AJAX config and jstree will automatically make an AJAX request populate the tree with the response.
Add a class of jstree-closed
to any LI node you return and do not nest an UL node and jstree will make another AJAX call as soon as the user opens this node.
In addition to the standard jQuery ajax options here you can supply functions for data
and url
, the functions will be run in the current instance's scope and a param will be passed indicating which node is being loaded, the return value of those functions will be used as URL and data respectively.
$('#tree').jstree({
'core' : {
'data' : {
'url' : 'ajax_nodes.html',
'data' : function (node) {
'url' : 'ajax_nodes.html',
'data' : function (node) {
return { 'id' : node.id };
}
}
}
});
// Example response:
<ul>
<li>Node 1</li>
<li class="jstree-closed">Node 2</li>
</ul>
Populating the tree using JSON
The format
jsTree needs a specific format to work with JSON. In the standard syntax no fields are required - pass only what you need. Keep in mind you will be able to access any additional properties you specify - jsTree won't touch them and you will be able to use them later on.
To change the icon of the node use the icon
property. Specifying a string containing a /
will display that image as the node icon. Using any other string will apply that class to the <i>
element that is used to represent the icon. You can use boolean false
to make jsTree render the node with no icon.
You can set the state on a node using the state
property. Use any combination of the following: opened
, selected
, disabled
.
Both li_attr
and a_attr
are passed directly to jQuery's attr function.
When using AJAX set children
to boolean true
and jsTree will render the node as closed and make an additional request for that node when the user opens it.
Any nested children should either be objects following the same format, or plain strings (in which case the string is used for the node's text and everything else is autogenerated).
// Expected format of the node (there are no required fields)
{
id : "string" // will be autogenerated if omitted
text : "string" // node text
icon : "string" // string for custom
state : {
opened : boolean // is the node open
disabled : boolean // is the node disabled
selected : boolean // is the node selected
},
children : [] // array of strings or objects
li_attr : {} // attributes for the generated LI node
a_attr : {} // attributes for the generated A node
}
Alternative JSON format
If you do not want to use the nested children
approach, you can use the alternative syntax where each node object has two required fields: id
& parent
and no children
property (everything else remains the same).
jsTree will automatically build the hierarchy. To indicate a node should be a root node set its parent
property to "#"
.
This should be used mainly when you render the whole tree at once and is useful when data is stored in a database using adjacency.
// Alternative format of the node (id & parent are required)
{
id : "string" // required
parent : "string" // required
text : "string" // node text
icon : "string" // string for custom
state : {
opened : boolean // is the node open
disabled : boolean // is the node disabled
selected : boolean // is the node selected
},
li_attr : {} // attributes for the generated LI node
a_attr : {} // attributes for the generated A node
}
Using JSON
To populate the tree with a JSON object you need to use the $.jstree.defaults.core.data
config option.
The expected format is an array of nodes, where each node should be an object as described above or a simple string (in which case the string is used for the node's text property ane everything else is autogenerated). Any nested nodes are supplied in the same manner in the children
property of their parent.
$('#using_json').jstree({ 'core' : {
'data' : [
'Simple root node',
{
'text' : 'Root node 2',
'state' : {
'opened' : true,
'selected' : true
},
'children' : [
{ 'text' : 'Child 1' },
'Child 2'
]
}
]
} });
Using the alternative JSON format
$('#using_json_2').jstree({ 'core' : {
'data' : [
{ "id" : "ajson1", "parent" : "#", "text" : "Simple root node" },
{ "id" : "ajson2", "parent" : "#", "text" : "Root node 2" },
{ "id" : "ajson3", "parent" : "ajson2", "text" : "Child 1" },
{ "id" : "ajson4", "parent" : "ajson2", "text" : "Child 2" },
]
} });
Using AJAX
You can also use AJAX to populate the tree with JSON your server returns. The format remains the same as the above, the only difference is that the JSON is not inside the config object, but returned from the server.
To take advantage of this option you need to use the $.jstree.defaults.core.data
config option.
Just use a standard jQuery-like AJAX config and jstree will automatically make an AJAX request populate the tree with the response.
In addition to the standard jQuery ajax options here you can supply functions for data
and url
, the functions will be run in the current instance's scope and a param will be passed indicating which node is being loaded, the return value of those functions will be used as URL and data respectively.
If you do not return correct json headers from the server, at least set the dataType
jQuery AJAX option to "json"
.
$('#tree').jstree({
'core' : {
'data' : {
'url' : function (node) {
return node.id === '#' ?
'ajax_roots.json' :
'ajax_children.json';
},
'data' : function (node) {
return { 'id' : node.id };
}
}
});
Using a function
Listening for events
jsTree triggers various events on the container. You can review the list of all events to know what to listen for.
To get more information about the event inspect its data
argument.
In most cases where a node is involved you will get the whole node object passed in. If you get an ID string somewhere and want to inspect the node just use .get_node().
$('#jstree')
// listen for event
.on('changed.jstree', function (e, data) {
var i, j, r = [];
for(i = 0, j = data.selected.length; i < j; i++) {
r.push(data.instance.get_node(data.selected[i]).text);
}
$('#event_result').html('Selected: ' + r.join(', '));
})
// create the instance
.jstree();
Invoking methods on an instance
true
To invoke a method on an instance you must obtain a reference of the instance and invoke the method. The example shows how to obtain a reference and invoke a method.
Check the API for a list of available methods.
// 3 ways of doing the same thing
$('#jstree').jstree(true)
.select_node('mn1');
$('#jstree')
.jstree('select_node', 'mn2');
$.jstree.reference('#jstree')
.select_node('mn3');
Basic AJAX demo
$('#jstree_demo').jstree({
"core" : {
"animation" : 0,
"check_callback" : true,
"themes" : { "stripes" : true },
'data' : {
'url' : function (node) {
return node.id === '#' ?
'ajax_demo_roots.json' : 'ajax_demo_children.json';
},
'data' : function (node) {
return { 'id' : node.id };
}
}
},
"types" : {
"#" : {
"max_children" : 1,
"max_depth" : 4,
"valid_children" : ["root"]
},
"root" : {
"icon" : "./assets/images/tree_icon.png",
"valid_children" : ["default"]
},
"default" : {
"valid_children" : ["default","file"]
},
"file" : {
"icon" : "glyphicon glyphicon-file",
"valid_children" : []
}
},
"plugins" : [
"contextmenu", "dnd", "search",
"state", "types", "wholerow"
]
});
demo
folder of the downloadFilebrowser demo
Database demo
Sorry, no results found.
This is a filtered view.
jsTree core functionality
$.jstree
holds all jstree related functions and variables, including the actual class and methods to create, access and manipulate instances.
$.jstree.version
specifies the jstree version in use
$.jstree.defaults
holds all the default options used when creating new instances
$.jstree.defaults.plugins
configure which plugins will be active on an instance. Should be an array of strings, where each element is a plugin name. The default is []
$.jstree.plugins
stores all loaded jstree plugins (used internally)
$.jstree.create (el [, options])
creates a jstree instance
el
DOMElement
jQuery
String
the element to create the instance on, can be jQuery extended or a selectoroptions
Object
options for this instance (extends `$.jstree.defaults`)
Returns
jsTree
the new instance
$.jstree.core (id)
private
the jstree class constructor, used only internally
id
Number
this instance's index
$.jstree.reference (needle)
get a reference to an existing instance
needle
DOMElement
jQuery
String
Returns
jsTree
null
the instance or `null` if not found
Examples
// provided a container with an ID of "tree", and a nested node with an ID of "branch"
// all of there will return the same instance
$.jstree.reference('tree');
$.jstree.reference('#tree');
$.jstree.reference($('#tree'));
$.jstree.reference(document.getElementByID('tree'));
$.jstree.reference('branch');
$.jstree.reference('#branch');
$.jstree.reference($('#branch'));
$.jstree.reference(document.getElementByID('branch'));
$().jstree([arg])
Create an instance, get an instance or invoke a command on a instance.
If there is no instance associated with the current node a new one is created and arg
is used to extend $.jstree.defaults
for this new instance. There would be no return value (chaining is not broken).
If there is an existing instance and arg
is a string the command specified by arg
is executed on the instance, with any additional arguments passed to the function. If the function returns a value it will be returned (chaining could break depending on function).
If there is an existing instance and arg
is not a string the instance itself is returned (similar to $.jstree.reference
).
In any other case - nothing is returned and chaining is not broken.
arg
String
Object
Returns
Mixed
Examples
$('#tree1').jstree(); // creates an instance
$('#tree2').jstree({ plugins : [] }); // create an instance with some options
$('#tree1').jstree('open_node', '#branch_1'); // call a method on an existing instance, passing additional arguments
$('#tree2').jstree(); // get an existing instance (or create an instance)
$('#tree2').jstree(true); // get an existing instance (will not create new instance)
$('#branch_1').jstree().select_node('#branch_1'); // get an instance (using a nested element and call a method)
$(':jstree')
used to find elements containing an instance
Returns
jQuery
Examples
$('div:jstree').each(function () {
$(this).jstree('destroy');
});
$.jstree.defaults.core
stores all defaults for the core
$.jstree.defaults.core.data
data configuration
If left as false
the HTML inside the jstree container element is used to populate the tree (that should be an unordered list with list items).
You can also pass in a HTML string or a JSON array here.
It is possible to pass in a standard jQuery-like AJAX config and jstree will automatically determine if the response is JSON or HTML and use that to populate the tree.
In addition to the standard jQuery ajax options here you can suppy functions for data
and url
, the functions will be run in the current instance's scope and a param will be passed indicating which node is being loaded, the return value of those functions will be used.
The last option is to specify a function, that function will receive the node being loaded as argument and a second param which is a function which should be called with the result.
Examples
// AJAX
$('#tree').jstree({
'core' : {
'data' : {
'url' : '/get/children/',
'data' : function (node) {
return { 'id' : node.id };
}
}
});
// direct data
$('#tree').jstree({
'core' : {
'data' : [
'Simple root node',
{
'id' : 'node_2',
'text' : 'Root node with options',
'state' : { 'opened' : true, 'selected' : true },
'children' : [ { 'text' : 'Child 1' }, 'Child 2']
}
]
});
// function
$('#tree').jstree({
'core' : {
'data' : function (obj, callback) {
callback.call(this, ['Root 1', 'Root 2']);
}
});
$.jstree.defaults.core.strings
configure the various strings used throughout the tree
You can use an object where the key is the string you need to replace and the value is your replacement.
Another option is to specify a function which will be called with an argument of the needed string and should return the replacement.
If left as false
no replacement is made.
Examples
$('#tree').jstree({
'core' : {
'strings' : {
'Loading...' : 'Please wait ...'
}
}
});
$.jstree.defaults.core.check_callback
determines what happens when a user tries to modify the structure of the tree
If left as false
all operations like create, rename, delete, move or copy are prevented.
You can set this to true
to allow all interactions or use a function to have better control.
Examples
$('#tree').jstree({
'core' : {
'check_callback' : function (operation, node, node_parent, node_position, more) {
// operation can be 'create_node', 'rename_node', 'delete_node', 'move_node' or 'copy_node'
// in case of 'rename_node' node_position is filled with the new node name
return operation === 'rename_node' ? true : false;
}
}
});
$.jstree.defaults.core.error
a callback called with a single object parameter in the instance's scope when something goes wrong (operation prevented, ajax failed, etc)
$.jstree.defaults.core.animation
the open / close animation duration in milliseconds - set this to false
to disable the animation (default is 200
)
$.jstree.defaults.core.multiple
a boolean indicating if multiple nodes can be selected
$.jstree.defaults.core.themes
theme configuration object
$.jstree.defaults.core.themes.name
the name of the theme to use (if left as false
the default theme is used)
$.jstree.defaults.core.themes.url
the URL of the theme's CSS file, leave this as false
if you have manually included the theme CSS (recommended). You can set this to true
too which will try to autoload the theme.
$.jstree.defaults.core.themes.dir
the location of all jstree themes - only used if url
is set to true
$.jstree.defaults.core.themes.dots
a boolean indicating if connecting dots are shown
$.jstree.defaults.core.themes.icons
a boolean indicating if node icons are shown
$.jstree.defaults.core.themes.stripes
a boolean indicating if the tree background is striped
$.jstree.defaults.core.themes.variant
a string (or boolean false
) specifying the theme variant to use (if the theme supports variants)
$.jstree.defaults.core.themes.responsive
a boolean specifying if a reponsive version of the theme should kick in on smaller screens (if the theme supports it). Defaults to true
.
$.jstree.defaults.core.expand_selected_onload
if left as true
all parents of all selected nodes will be opened once the tree loads (so that all selected nodes are visible to the user)
plugin (deco [, opts])
private
used to decorate an instance with a plugin. Used internally.
deco
String
the plugin to decorate withopts
Object
options for the plugin
Returns
jsTree
init (el, optons)
private
used to decorate an instance with a plugin. Used internally.
el
DOMElement
jQuery
String
the element we are transformingoptions
Object
options for this instance
Triggers
init.jstree
loading.jstree
loaded.jstree
ready.jstree
changed.jstree
init.jstree Event
triggered after all events are bound
loading.jstree Event
triggered after the loading text is shown and before loading starts
destroy ()
destroy an instance
teardown ()
private
part of the destroying of an instance. Used internally.
bind ()
private
bind all events. Used internally.
loaded.jstree Event
triggered after the root node is loaded for the first time
ready.jstree Event
triggered after all nodes are finished loading
unbind ()
private
part of the destroying of an instance. Used internally.
trigger (ev [, data])
private
trigger an event. Used internally.
ev
String
the name of the event to triggerdata
Object
additional data to pass with the event
get_container ()
returns the jQuery extended instance container
Returns
jQuery
get_container_ul ()
private
returns the jQuery extended main UL node inside the instance container. Used internally.
Returns
jQuery
get_string (key)
private
gets string replacements (localization). Used internally.
key
String
Returns
String
_firstChild (dom)
private
gets the first child of a DOM node. Used internally.
dom
DOMElement
Returns
DOMElement
_nextSibling (dom)
private
gets the next sibling of a DOM node. Used internally.
dom
DOMElement
Returns
DOMElement
_previousSibling (dom)
private
gets the previous sibling of a DOM node. Used internally.
dom
DOMElement
Returns
DOMElement
get_node (obj [, as_dom])
get the JSON representation of a node (or the actual jQuery extended DOM node) by using any input (child DOM element, ID string, selector, etc)
obj
mixed
as_dom
Boolean
Returns
Object
jQuery
get_path (obj [, glue, ids])
get the path to a node, either consisting of node texts, or of node IDs, optionally glued together (otherwise an array)
obj
mixed
the nodeglue
String
if you want the path as a string - pass the glue here (for example '/'), if a falsy value is supplied here, an array is returnedids
Boolean
if set to true build the path using ID, otherwise node text is used
Returns
mixed
get_next_dom (obj [, strict])
get the next visible node that is below the obj
node. If strict
is set to true
only sibling nodes are returned.
obj
mixed
strict
Boolean
Returns
jQuery
get_prev_dom (obj [, strict])
get the previous visible node that is above the obj
node. If strict
is set to true
only sibling nodes are returned.
obj
mixed
strict
Boolean
Returns
jQuery
get_parent (obj)
get the parent ID of a node
obj
mixed
Returns
String
get_children_dom (obj)
get a jQuery collection of all the children of a node (node must be rendered)
obj
mixed
Returns
jQuery
is_parent (obj)
checks if a node has children
obj
mixed
Returns
Boolean
is_loaded (obj)
checks if a node is loaded (its children are available)
obj
mixed
Returns
Boolean
is_loading (obj)
check if a node is currently loading (fetching children)
obj
mixed
Returns
Boolean
is_open (obj)
check if a node is opened
obj
mixed
Returns
Boolean
is_closed (obj)
check if a node is in a closed state
obj
mixed
Returns
Boolean
is_leaf (obj)
check if a node has no children
obj
mixed
Returns
Boolean
load_node (obj [, callback])
loads a node (fetches its children using the core.data
setting). Multiple nodes can be passed to by using an array.
obj
mixed
callback
function
a function to be executed once loading is conplete, the function is executed in the instance's scope and receives two arguments - the node and a boolean status
Returns
Boolean
Triggers
load_node.jstree
load_node.jstree Event
triggered after a node is loaded
node
Object
the node that was loadingstatus
Boolean
was the node loaded successfully
_load_nodes (nodes [, callback])
private
load an array of nodes (will also load unavailable nodes as soon as the appear in the structure). Used internally.
nodes
array
callback
function
a function to be executed once loading is complete, the function is executed in the instance's scope and receives one argument - the array passed to _load_nodes
_load_node (obj [, callback])
private
handles the actual loading of a node. Used only internally.
obj
mixed
callback
function
a function to be executed once loading is complete, the function is executed in the instance's scope and receives one argument - a boolean status
Returns
Boolean
_node_changed (obj [, callback])
private
adds a node to the list of nodes to redraw. Used only internally.
obj
mixed
_append_html_data (obj, data)
private
appends HTML content to the tree. Used internally.
obj
mixed
the node to append todata
String
the HTML string to parse and append
Returns
Boolean
Triggers
model.jstree
changed.jstree
model.jstree Event
triggered when new data is inserted to the tree model
nodes
Array
an array of node IDsparent
String
the parent ID of the nodes
_append_json_data (obj, data)
private
appends JSON content to the tree. Used internally.
obj
mixed
the node to append todata
String
the JSON object to parse and append
Returns
Boolean
_parse_model_from_html (d [, p, ps])
private
parses a node from a jQuery object and appends them to the in memory tree model. Used internally.
d
jQuery
the jQuery object to parsep
String
the parent IDps
Array
list of all parents
Returns
String
the ID of the object added to the model
_parse_model_from_flat_json (d [, p, ps])
private
parses a node from a JSON object (used when dealing with flat data, which has no nesting of children, but has id and parent properties) and appends it to the in memory tree model. Used internally.
d
Object
the JSON object to parsep
String
the parent IDps
Array
list of all parents
Returns
String
the ID of the object added to the model
_parse_model_from_json (d [, p, ps])
private
parses a node from a JSON object and appends it to the in memory tree model. Used internally.
d
Object
the JSON object to parsep
String
the parent IDps
Array
list of all parents
Returns
String
the ID of the object added to the model
_redraw ()
private
redraws all nodes that need to be redrawn. Used internally.
Triggers
redraw.jstree
redraw.jstree Event
triggered after nodes are redrawn
nodes
array
the redrawn nodes
redraw ([full])
redraws all nodes that need to be redrawn or optionally - the whole tree
full
Boolean
if set to `true` all nodes are redrawn.
redraw_node (node, deep, is_callback)
private
redraws a single node. Used internally.
node
mixed
the node to redrawdeep
Boolean
should child nodes be redrawn toois_callback
Boolean
is this a recursion call
open_node (obj [, callback, animation])
opens a node, revaling its children. If the node is not loaded it will be loaded and opened once ready.
obj
mixed
the node to opencallback
Function
a function to execute once the node is openedanimation
Number
the animation duration in milliseconds when opening the node (overrides the `core.animation` setting). Use `false` for no animation.
Triggers
open_node.jstree
after_open.jstree
before_open.jstree
before_open.jstree Event
triggered when a node is about to be opened (if the node is supposed to be in the DOM, it will be, but it won't be visible yet)
node
Object
the opened node
open_node.jstree Event
triggered when a node is opened (if there is an animation it will not be completed yet)
node
Object
the opened node
after_open.jstree Event
triggered when a node is opened and the animation is complete
node
Object
the opened node
_open_to (obj)
private
opens every parent of a node (node should be loaded)
obj
mixed
the node to reveal
close_node (obj [, animation])
closes a node, hiding its children
obj
mixed
the node to closeanimation
Number
the animation duration in milliseconds when closing the node (overrides the `core.animation` setting). Use `false` for no animation.
Triggers
close_node.jstree
after_close.jstree
close_node.jstree Event
triggered when a node is closed (if there is an animation it will not be complete yet)
node
Object
the closed node
after_close.jstree Event
triggered when a node is closed and the animation is complete
node
Object
the closed node
toggle_node (obj)
toggles a node - closing it if it is open, opening it if it is closed
obj
mixed
the node to toggle
open_all ([obj, animation, original_obj])
opens all nodes within a node (or the tree), revaling their children. If the node is not loaded it will be loaded and opened once ready.
obj
mixed
the node to open recursively, omit to open all nodes in the treeanimation
Number
the animation duration in milliseconds when opening the nodes, the default is no animationreference
jQuery
to the node that started the process (internal use)
Triggers
open_all.jstree
open_all.jstree Event
triggered when an open_all
call completes
node
Object
the opened node
close_all ([obj, animation])
closes all nodes within a node (or the tree), revaling their children
obj
mixed
the node to close recursively, omit to close all nodes in the treeanimation
Number
the animation duration in milliseconds when closing the nodes, the default is no animation
Triggers
close_all.jstree
close_all.jstree Event
triggered when an close_all
call completes
node
Object
the closed node
is_disabled (obj)
checks if a node is disabled (not selectable)
obj
mixed
Returns
Boolean
enable_node (obj)
enables a node - so that it can be selected
obj
mixed
the node to enable
Triggers
enable_node.jstree
enable_node.jstree Event
triggered when an node is enabled
node
Object
the enabled node
disable_node (obj)
disables a node - so that it can not be selected
obj
mixed
the node to disable
Triggers
disable_node.jstree
disable_node.jstree Event
triggered when an node is disabled
node
Object
the disabled node
activate_node (obj, e)
private
called when a node is selected by the user. Used internally.
obj
mixed
the nodee
Object
the related event
Triggers
activate_node.jstree
activate_node.jstree Event
triggered when an node is clicked or intercated with by the user
node
Object
hover_node (obj)
private
applies the hover state on a node, called when a node is hovered by the user. Used internally.
obj
mixed
Triggers
hover_node.jstree
hover_node.jstree Event
triggered when an node is hovered
node
Object
dehover_node (obj)
private
removes the hover state from a nodecalled when a node is no longer hovered by the user. Used internally.
obj
mixed
Triggers
dehover_node.jstree
dehover_node.jstree Event
triggered when an node is no longer hovered
node
Object
select_node (obj [, supress_event, prevent_open])
select a node
obj
mixed
an array can be used to select multiple nodessupress_event
Boolean
if set to `true` the `changed.jstree` event won't be triggeredprevent_open
Boolean
if set to `true` parents of the selected node won't be opened
Triggers
select_node.jstree
changed.jstree
select_node.jstree Event
triggered when an node is selected
node
Object
selected
Array
the current selectionevent
Object
the event (if any) that triggered this select_node
changed.jstree Event
triggered when selection changes
node
Object
action
Object
the action that caused the selection to changeselected
Array
the current selectionevent
Object
the event (if any) that triggered this changed event
deselect_node (obj [, supress_event])
deselect a node
obj
mixed
an array can be used to deselect multiple nodessupress_event
Boolean
if set to `true` the `changed.jstree` event won't be triggered
Triggers
deselect_node.jstree
changed.jstree
deselect_node.jstree Event
triggered when an node is deselected
node
Object
selected
Array
the current selectionevent
Object
the event (if any) that triggered this deselect_node
select_all ([supress_event])
select all nodes in the tree
supress_event
Boolean
if set to `true` the `changed.jstree` event won't be triggered
Triggers
select_all.jstree
changed.jstree
select_all.jstree Event
triggered when all nodes are selected
selected
Array
the current selection
deselect_all ([supress_event])
deselect all selected nodes
supress_event
Boolean
if set to `true` the `changed.jstree` event won't be triggered
Triggers
deselect_all.jstree
changed.jstree
deselect_all.jstree Event
triggered when all nodes are deselected
node
Object
the previous selectionselected
Array
the current selection
is_selected (obj)
checks if a node is selected
obj
mixed
Returns
Boolean
get_selected ([full])
get an array of all selected nodes
full
mixed
if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
Returns
Array
get_top_selected ([full])
get an array of all top level selected nodes (ignoring children of selected nodes)
full
mixed
if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
Returns
Array
get_top_selected ([full])
get an array of all bottom level selected nodes (ignoring selected parents)
full
mixed
if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned
Returns
Array
get_state ()
private
gets the current state of the tree so that it can be restored later with set_state(state)
. Used internally.
Returns
Object
set_state (state [, callback])
private
sets the state of the tree. Used internally.
state
Object
the state to restorecallback
Function
an optional function to execute once the state is restored.
Triggers
set_state.jstree
set_state.jstree Event
triggered when a set_state
call completes
refresh ()
refreshes the tree - all nodes are reloaded with calls to load_node
.
skip_loading
Boolean
an option to skip showing the loading indicator
Triggers
refresh.jstree
refresh.jstree Event
triggered when a refresh
call completes
refresh_name (obj)
refreshes a node in the tree (reload its children) all opened nodes inside that node are reloaded with calls to load_node
.
skip_loading
Boolean
an option to skip showing the loading indicator
Triggers
refresh.jstree
move_node.jstree Event
triggered when a node is refreshed
node
Object
- the refreshed nodenodes
Array
- an array of the IDs of the nodes that were reloaded
set_id (obj, id)
set (change) the ID of a node
obj
mixed
the nodeid
String
the new ID
Returns
Boolean
get_text (obj)
get the text value of a node
obj
mixed
the node
Returns
String
set_text (obj, val)
private
set the text value of a node. Used internally, please use rename_node(obj, val)
.
obj
mixed
the node, you can pass an array to set the text on multiple nodesval
String
the new text value
Returns
Boolean
Triggers
set_text.jstree
set_text.jstree Event
triggered when a node text value is changed
obj
Object
text
String
the new value
get_json ([obj, options])
gets a JSON representation of a node (or the whole tree)
obj
mixed
options
Object
options.no_state
Boolean
do not return state informationoptions.no_id
Boolean
do not return IDoptions.no_children
Boolean
do not include childrenoptions.no_data
Boolean
do not include node dataoptions.flat
Boolean
return flat JSON instead of nested
Returns
Object
create_node ([obj, node, pos, callback, is_loaded])
create a new node (do not confuse with load_node)
par
mixed
the parent node (to create a root node use either "#" (string) or `null`)node
mixed
the data for the new node (a valid JSON object, or a simple string with the name)pos
mixed
the index at which to insert the node, "first" and "last" are also supported, default is "last"callback
Function
a function to be called once the node is createdis_loaded
Boolean
internal argument indicating if the parent node was succesfully loaded
Returns
String
the ID of the newly create node
Triggers
model.jstree
create_node.jstree
create_node.jstree Event
triggered when a node is created
node
Object
parent
String
the parent's IDposition
Number
the position of the new node among the parent's children
rename_node (obj, val)
set the text value of a node
obj
mixed
the node, you can pass an array to rename multiple nodes to the same nameval
String
the new text value
Returns
Boolean
Triggers
rename_node.jstree
rename_node.jstree Event
triggered when a node is renamed
node
Object
text
String
the new valueold
String
the old value
delete_node (obj)
remove a node
obj
mixed
the node, you can pass an array to delete multiple nodes
Returns
Boolean
Triggers
delete_node.jstree
changed.jstree
delete_node.jstree Event
triggered when a node is deleted
node
Object
parent
String
the parent's ID
check (chk, obj, par, pos)
private
check if an operation is premitted on the tree. Used internally.
chk
String
the operation to check, can be "create_node", "rename_node", "delete_node", "copy_node" or "move_node"obj
mixed
the nodepar
mixed
the parentpos
mixed
the position to insert at, or if "rename_node" - the new namemore
mixed
some various additional information, for example if a "move_node" operations is triggered by DND this will be the hovered node
Returns
Boolean
last_error ()
get the last error
Returns
Object
move_node (obj, par [, pos, callback, is_loaded])
move a node to a new parent
obj
mixed
the node to move, pass an array to move multiple nodespar
mixed
the new parentpos
mixed
the position to insert at (besides integer values, "first" and "last" are supported, as well as "before" and "after"), defaults to integer `0`callback
function
a function to call once the move is completed, receives 3 arguments - the node, the new parent and the positioninternal
Boolean
parameter indicating if the parent node has been loaded
Triggers
move_node.jstree
move_node.jstree Event
triggered when a node is moved
node
Object
parent
String
the parent's IDposition
Number
the position of the node among the parent's childrenold_parent
String
the old parent of the nodeis_multi
Boolean
do the node and new parent belong to different instancesold_instance
jsTree
the instance the node came fromnew_instance
jsTree
the instance of the new parent
copy_node (obj, par [, pos, callback, is_loaded])
copy a node to a new parent
obj
mixed
the node to copy, pass an array to copy multiple nodespar
mixed
the new parentpos
mixed
the position to insert at (besides integer values, "first" and "last" are supported, as well as "before" and "after"), defaults to integer `0`callback
function
a function to call once the move is completed, receives 3 arguments - the node, the new parent and the positioninternal
Boolean
parameter indicating if the parent node has been loaded
Triggers
model.jstree copy_node.jstree
copy_node.jstree Event
triggered when a node is copied
node
Object
the copied nodeoriginal
Object
the original nodeparent
String
the parent's IDposition
Number
the position of the node among the parent's childrenold_parent
String
the old parent of the nodeis_multi
Boolean
do the node and new parent belong to different instancesold_instance
jsTree
the instance the node came fromnew_instance
jsTree
the instance of the new parent
cut (obj)
cut a node (a later call to paste(obj)
would move the node)
obj
mixed
multiple objects can be passed using an array
Triggers
cut.jstree
cut.jstree Event
triggered when nodes are added to the buffer for moving
node
Array
copy (obj)
copy a node (a later call to paste(obj)
would copy the node)
obj
mixed
multiple objects can be passed using an array
Triggers
copy.jstre
copy.jstree Event
triggered when nodes are added to the buffer for copying
node
Array
get_buffer ()
get the current buffer (any nodes that are waiting for a paste operation)
Returns
Object
an object consisting of `mode` ("copy_node" or "move_node"), `node` (an array of objects) and `inst` (the instance)
can_paste ()
check if there is something in the buffer to paste
Returns
Boolean
paste (obj [, pos])
copy or move the previously cut or copied nodes to a new parent
obj
mixed
the new parentpos
mixed
the position to insert at (besides integer, "first" and "last" are supported), defaults to integer `0`
Triggers
paste.jstree
paste.jstree Event
triggered when paste is invoked
parent
String
the ID of the receiving nodenode
Array
the nodes in the buffermode
String
the performed operation - "copy_node" or "move_node"
edit (obj [, default_text])
put a node in edit mode (input field to rename the node)
obj
mixed
default_text
String
the text to populate the input with (if omitted the node text value is used)
set_theme (theme_name [, theme_url])
changes the theme
theme_name
String
the name of the new theme to applytheme_url
mixed
the location of the CSS file for this theme. Omit or set to `false` if you manually included the file. Set to `true` to autoload from the `core.themes.dir` directory.
Triggers
set_theme.jstree
set_theme.jstree Event
triggered when a theme is set
theme
String
the new theme
get_theme ()
gets the name of the currently applied theme name
Returns
String
set_theme_variant (variant_name)
changes the theme variant (if the theme has variants)
variant_name
String
Boolean
the variant to apply (if `false` is used the current variant is removed)
get_theme ()
gets the name of the currently applied theme variant
Returns
String
show_stripes ()
shows a striped background on the container (if the theme supports it)
hide_stripes ()
hides the striped background on the container
toggle_stripes ()
toggles the striped background on the container
show_dots ()
shows the connecting dots (if the theme supports it)
hide_dots ()
hides the connecting dots
toggle_dots ()
toggles the connecting dots
show_icons ()
show the node icons
hide_icons ()
hide the node icons
toggle_icons ()
toggle the node icons
set_icon (obj, icon)
set the node icon for a node
obj
mixed
icon
String
the new icon - can be a path to an icon or a className, if using an image that is in the current directory use a `./` prefix, otherwise it will be detected as a class
get_icon (obj)
get the node icon for a node
obj
mixed
Returns
String
hide_icon (obj)
hide the icon on an individual node
obj
mixed
show_icon (obj)
show the icon on an individual node
obj
mixed
Checkbox plugin
This plugin renders checkbox icons in front of each node, making multiple selection much easier.
It also supports tri-state behavior, meaning that if a node has a few of its children checked it will be rendered as undetermined, and state will be propagated up.
$.jstree.defaults.checkbox
checkbox plugin
stores all defaults for the checkbox plugin
$.jstree.defaults.checkbox.visible
checkbox plugin
a boolean indicating if checkboxes should be visible (can be changed at a later time using show_checkboxes()
and hide_checkboxes
). Defaults to true
.
$.jstree.defaults.checkbox.three_state
checkbox plugin
a boolean indicating if checkboxes should cascade down and have an undetermined state. Defaults to true
.
$.jstree.defaults.checkbox.whole_node
checkbox plugin
a boolean indicating if clicking anywhere on the node should act as clicking on the checkbox. Defaults to true
.
$.jstree.defaults.checkbox.keep_selected_style
checkbox plugin
a boolean indicating if the selected style of a node should be kept, or removed. Defaults to true
.
_undetermined ()
checkbox plugin
private
set the undetermined state where and if necessary. Used internally.
show_checkboxes ()
checkbox plugin
show the node checkbox icons
hide_checkboxes ()
checkbox plugin
hide the node checkbox icons
toggle_checkboxes ()
checkbox plugin
toggle the node icons
Contextmenu plugin
Shows a context menu when a node is right-clicked.
$.jstree.defaults.contextmenu
contextmenu plugin
stores all defaults for the contextmenu plugin
$.jstree.defaults.contextmenu.select_node
contextmenu plugin
a boolean indicating if the node should be selected when the context menu is invoked on it. Defaults to true
.
$.jstree.defaults.contextmenu.show_at_node
contextmenu plugin
a boolean indicating if the menu should be shown aligned with the node. Defaults to true
, otherwise the mouse coordinates are used.
$.jstree.defaults.contextmenu.items
contextmenu plugin
an object of actions, or a function that accepts a node and a callback function and calls the callback function with an object of actions available for that node (you can also return the items too).
Each action consists of a key (a unique name) and a value which is an object with the following properties (only label and action are required):
separator_before
- a boolean indicating if there should be a separator before this itemseparator_after
- a boolean indicating if there should be a separator after this item_disabled
- a boolean indicating if this action should be disabledlabel
- a string - the name of the action (could be a function returning a string)action
- a function to be executed if this item is chosenicon
- a string, can be a path to an icon or a className, if using an image that is in the current directory use a./
prefix, otherwise it will be detected as a classshortcut
- keyCode which will trigger the action if the menu is open (for example113
for rename, which equals F2)shortcut_label
- shortcut label (like for exampleF2
for rename)
"shortcut" : 113,
"shortcut_label" : 'F2',
"icon" : "glyphicon glyphicon-leaf",
if(!('oncontextmenu' in document.body) && ('ontouchstart' in document.body)) {
var el = null, tm = null;
this.element
.on("touchstart", ".jstree-anchor", function (e) {
el = e.currentTarget;
tm = +new Date();
$(document).one("touchend", function (e) {
e.target = document.elementFromPoint(e.originalEvent.targetTouches[0].pageX - window.pageXOffset, e.originalEvent.targetTouches[0].pageY - window.pageYOffset);
e.currentTarget = e.target;
tm = ((+(new Date())) - tm);
if(e.target === el && tm > 600 && tm < 1000) {
e.preventDefault();
$(el).trigger('contextmenu', e);
}
el = null;
tm = null;
});
});
}
show_contextmenu (obj [, x, y])
contextmenu plugin
prepare and show the context menu for a node
obj
mixed
the nodex
Number
the x-coordinate relative to the document to show the menu aty
Number
the y-coordinate relative to the document to show the menu ate
Object
the event if available that triggered the contextmenu
Triggers
show_contextmenu.jstree
_show_contextmenu (obj, x, y, i)
contextmenu plugin
private
show the prepared context menu for a node
obj
mixed
the nodex
Number
the x-coordinate relative to the document to show the menu aty
Number
the y-coordinate relative to the document to show the menu ati
Number
the object of items to show
Triggers
show_contextmenu.jstree
show_contextmenu.jstree Event
contextmenu plugin
triggered when the contextmenu is shown for a node
node
Object
the nodex
Number
the x-coordinate of the menu relative to the documenty
Number
the y-coordinate of the menu relative to the document
context_parse.vakata Event
contextmenu plugin
triggered on the document when the contextmenu is parsed (HTML is built)
reference
jQuery
the element that was right clickedelement
jQuery
the DOM element of the menu itselfposition
Object
the x & y coordinates of the menu
context_show.vakata Event
contextmenu plugin
triggered on the document when the contextmenu is shown
reference
jQuery
the element that was right clickedelement
jQuery
the DOM element of the menu itselfposition
Object
the x & y coordinates of the menu
context_hide.vakata Event
contextmenu plugin
triggered on the document when the contextmenu is hidden
reference
jQuery
the element that was right clickedelement
jQuery
the DOM element of the menu itselfposition
Object
the x & y coordinates of the menu
Drag'n'drop plugin
Enables dragging and dropping of nodes in the tree, resulting in a move or copy operations.
$.jstree.defaults.dnd
dnd plugin
stores all defaults for the drag'n'drop plugin
$.jstree.defaults.dnd.copy
dnd plugin
a boolean indicating if a copy should be possible while dragging (by pressint the meta key or Ctrl). Defaults to true
.
$.jstree.defaults.dnd.open_timeout
dnd plugin
a number indicating how long a node should remain hovered while dragging to be opened. Defaults to 500
.
$.jstree.defaults.dnd.is_draggable
dnd plugin
a function invoked each time a node is about to be dragged, invoked in the tree's scope and receives the nodes about to be dragged as an argument (array) - return false
to prevent dragging
$.jstree.defaults.dnd.check_while_dragging
dnd plugin
a boolean indicating if checks should constantly be made while the user is dragging the node (as opposed to checking only on drop), default is true
$.jstree.defaults.dnd.always_copy
dnd plugin
a boolean indicating if nodes from this tree should only be copied with dnd (as opposed to moved), default is false
dnd_scroll.vakata Event
dnd plugin
triggered on the document when a drag causes an element to scroll
data
Mixed
any data supplied with the call to $.vakata.dnd.startelement
DOM
the DOM element being draggedhelper
jQuery
the helper shown next to the mouseevent
jQuery
the element that is scrolling
dnd_start.vakata Event
dnd plugin
triggered on the document when a drag starts
data
Mixed
any data supplied with the call to $.vakata.dnd.startelement
DOM
the DOM element being draggedhelper
jQuery
the helper shown next to the mouseevent
Object
the event that caused the start (probably mousemove)
dnd_move.vakata Event
dnd plugin
triggered on the document when a drag is in progress
data
Mixed
any data supplied with the call to $.vakata.dnd.startelement
DOM
the DOM element being draggedhelper
jQuery
the helper shown next to the mouseevent
Object
the event that caused this to trigger (most likely mousemove)
dnd_stop.vakata Event
dnd plugin
triggered on the document when a drag stops (the dragged element is dropped)
data
Mixed
any data supplied with the call to $.vakata.dnd.startelement
DOM
the DOM element being draggedhelper
jQuery
the helper shown next to the mouseevent
Object
the event that caused the stop
Search plugin
Adds search functionality to jsTree.
$.jstree.defaults.search
search plugin
stores all defaults for the search plugin
$.jstree.defaults.search.ajax
search plugin
a jQuery-like AJAX config, which jstree uses if a server should be queried for results.
A str
(which is the search string) parameter will be added with the request. The expected result is a JSON array with nodes that need to be opened so that matching nodes will be revealed.
Leave this setting as false
to not query the server. You can also set this to a function, which will be invoked in the instance's scope and receive 2 parameters - the search string and the callback to call with the array of nodes to load.
$.jstree.defaults.search.fuzzy
search plugin
Indicates if the search should be fuzzy or not (should chnd3
match child node 3
). Default is true
.
$.jstree.defaults.search.case_sensitive
search plugin
Indicates if the search should be case sensitive. Default is false
.
$.jstree.defaults.search.show_only_matches
search plugin
Indicates if the tree should be filtered to show only matching nodes (keep in mind this can be a heavy on large trees in old browsers). Default is false
.
$.jstree.defaults.search.close_opened_onclear
search plugin
Indicates if all nodes opened to reveal the search result, should be closed when the search is cleared or a new search is performed. Default is true
.
$.jstree.defaults.search.search_leaves_only
search plugin
Indicates if only leaf nodes should be included in search results. Default is false
.
search (str [, skip_async])
search plugin
used to search the tree nodes for a given string
str
String
the search stringskip_async
Boolean
if set to true server will not be queried even if configured
Triggers
search.jstree
search.jstree Event
search plugin
triggered after search is complete
nodes
jQuery
a jQuery collection of matching nodesstr
String
the search stringres
Array
a collection of objects represeing the matching nodes
clear_search ()
search plugin
used to clear the last search (removes classes and shows all nodes if filtering is on)
Triggers
clear_search.jstree
clear_search.jstree Event
search plugin
triggered after search is complete
nodes
jQuery
a jQuery collection of matching nodes (the result from the last search)str
String
the search string (the last search string)res
Array
a collection of objects represeing the matching nodes (the result from the last search)
_search_open (d)
search plugin
private
opens nodes that need to be opened to reveal the search results. Used only internally.
d
Array
an array of node IDs
Sort plugin
Autmatically sorts all siblings in the tree according to a sorting function.
$.jstree.defaults.sort
sort plugin
the settings function used to sort the nodes.
It is executed in the tree's context, accepts two nodes as arguments and should return 1
or -1
.
sort (obj [, deep])
sort plugin
private
used to sort a node's children
obj
mixed
the nodedeep
Boolean
if set to `true` nodes are sorted recursively.
Triggers
search.jstree
State plugin
Saves the state of the tree (selected nodes, opened nodes) on the user's computer using available options (localStorage, cookies, etc)
$.jstree.defaults.state
state plugin
stores all defaults for the state plugin
$.jstree.defaults.state.key
state plugin
A string for the key to use when saving the current tree (change if using multiple trees in your project). Defaults to jstree
.
$.jstree.defaults.state.events
state plugin
A space separated list of events that trigger a state save. Defaults to changed.jstree open_node.jstree close_node.jstree
.
$.jstree.defaults.state.ttl
state plugin
Time in milliseconds after which the state will expire. Defaults to 'false' meaning - no expire.
$.jstree.defaults.state.filter
state plugin
A function that will be executed prior to restoring state with one argument - the state object. Can be used to clear unwanted parts of the state.
save_state ()
state plugin
save the state
restore_state ()
state plugin
restore the state from the user's computer
clear_state ()
state plugin
clear the state on the user's computer
Types plugin
Makes it possible to add predefined types for groups of nodes, which make it possible to easily control nesting rules and icon for each group.
$.jstree.defaults.types
types plugin
An object storing all types as key value pairs, where the key is the type name and the value is an object that could contain following keys (all optional).
max_children
the maximum number of immediate children this node type can have. Do not specify or set to-1
for unlimited.max_depth
the maximum number of nesting this node type can have. A value of1
would mean that the node can have children, but no grandchildren. Do not specify or set to-1
for unlimited.valid_children
an array of node type strings, that nodes of this type can have as children. Do not specify or set to-1
for no limits.icon
a string - can be a path to an icon or a className, if using an image that is in the current directory use a./
prefix, otherwise it will be detected as a class. Omit to use the default icon from your theme.
There are two predefined types:
#
represents the root of the tree, for examplemax_children
would control the maximum number of root nodes.default
represents the default node - any settings here will be applied to all nodes that do not have a type specified.
get_rules (obj)
types plugin
used to retrieve the type settings object for a node
obj
mixed
the node to find the rules for
Returns
Object
get_type (obj [, rules])
types plugin
used to retrieve the type string or settings object for a node
obj
mixed
the node to find the rules forrules
Boolean
if set to `true` instead of a string the settings object will be returned
Returns
String
Object
set_type (obj, type)
types plugin
used to change a node's type
obj
mixed
the node to changetype
String
the new type
Unique plugin
Enforces that no nodes with the same name can coexist as siblings.
Wholerow plugin
Makes each node appear block level. Making selection easier. May cause slow down for large trees in old browsers.
Plugins?
jsTree has some functionality moved out of the core so you can only use it when you need it. To enable a plugin use the plugins
config option and add that plugin's name to the array.
For example enabling all the plugins can be done this way:"plugins" : [ "checkbox", "contextmenu", "dnd", "search", "sort", "state", "types", "unique", "wholerow" ]
Here is a quick overview for each one.
Checkbox plugin
This plugin renders checkbox icons in front of each node, making multiple selection much easier.
It also supports tri-state behavior, meaning that if a node has a few of its children checked it will be rendered as undetermined, and state will be propagated up.
Undetermined state is automatically calculated, but if you are using AJAX and loading on demand and want to render a node as underemined pass "undetermined" : true
in its state.
You can find all the checkbox plugin config options in the API.
$(function () {
$("#plugins1").jstree({
"checkbox" : {
"keep_selected_style" : false
},
"plugins" : [ "checkbox" ]
});
});
Contextmenu plugin
This plugin makes it possible to right click nodes and shows a list of configurable actions in a menu.
You can find all the contextmenu plugin config options in the API.
$(function () {
$("#plugins2").jstree({
"core" : {
// so that create works
"check_callback" : true
},
"plugins" : [ "contextmenu" ]
});
});
Drag & drop plugin
This plugin makes it possible to drag and drop tree nodes and rearrange the tree.
You can find all the dnd plugin config options in the API.
$(function () {
$("#plugins3").jstree({
"core" : {
"check_callback" : true
},
"plugins" : [ "dnd" ]
});
});
Search plugin
This plugin adds the possibility to search for items in the tree and even to show only matching nodes.
You can find all the search plugin config options in the API.
$(function () {
$("#plugins4").jstree({
"plugins" : [ "search" ]
});
var to = false;
$('#plugins4_q').keyup(function () {
if(to) { clearTimeout(to); }
to = setTimeout(function () {
var v = $('#plugins4_q').val();
$('#plugins4').jstree(true).search(v);
}, 250);
});
});
Sort plugin
This plugin automatically arranges all sibling nodes according to a comparison config option function, which defaults to alphabetical order.
$(function () {
$("#plugins5").jstree({
"plugins" : [ "sort" ]
});
});
State plugin
This plugin saves all opened and selected nodes in the user's browser, so when returning to the same tree the previous state will be restored.
You can find all the state plugin config options in the API. Make a selection and refresh this page to see the change persisted.
$(function () {
$("#plugins6").jstree({
"state" : { "key" : "demo2" },
"plugins" : [ "state" ]
});
});
Types plugin
This plugin makes it possible to add predefined types for groups of nodes, which means to easily control nesting rules and icon for each group.
To set a node's type you can use set_type
or supply a type
property with the node's data.
You can find all the types plugin config options & functions in the API.
$(function () {
$("#plugins7").jstree({
"types" : {
"default" : {
"icon" : "glyphicon glyphicon-flash"
},
"demo" : {
"icon" : "glyphicon glyphicon-ok"
}
},
"plugins" : [ "types" ]
});
});
Unique plugin
Enforces that no nodes with the same name can coexist as siblings. This plugin has no options, it just prevents renaming and moving nodes to a parent, which already contains a node with the same name.
$(function () {
$("#plugins8").jstree({
"core" : {
"check_callback" : true
},
"plugins" : [ "unique", "dnd" ]
});
});
Wholerow plugin
Makes each node appear block level which makes selection easier. May cause slow down for large trees in old browsers.
$(function () {
$("#plugins9").jstree({
"plugins" : [ "wholerow" ]
});
});