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
- Root node 1
- initially selected
- custom icon URL
- initially open
- Another node
- Custom icon class (bootstrap)
- Root node 2
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(). The internal node object is very similar to the JSON format used for loading, but has a few extra properties, which might be useful: children
is an array of all the IDs of the node's immediate children, children_d
is an array of all the IDs of the node's descendants, parent
is the IDs of the node's parent and parents
is an array of all the IDs of the node's ancestors.
$('#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();
- Root 1
- Child 1
- Child 2
- Root 2
- Child 3
- Child 4