Enable or disable keyboard support. Default is enabled.
Example: disable keyboard support.
onCanMove
You can override this function to determine if a node can be moved.
onCanMoveTo
You can override this function to determine if a node can be moved to a certain position.
onCanSelectNode
You can set a function to override if a node can be selected. The function gets a node as parameter, and must return true or false.
For this to work, the option ‘selectable’ must be ‘true’.
onCreateLi
The function is called for each created node. You can use this to define extra html.
The function is called with the following parameters:
node: Node element
$li: Jquery li element
isSelected: is the node selected (true/false)
onDragMove
Function that is called when a node is dragged outside the tree. This function is called while the node is being dragged.
Also see the onDragStop option.
The function signature is function(node, event);
onDragStop
Function that is called when a node is dragged outside the tree. This function is called when user stops dragging.
Also see the onDragMove option.
The function signature is function(node, event);
onIsMoveHandle
You can override this function to determine if a dom element can be used to move a node.
onLoadFailed
When loading the data by ajax fails, then the option onLoadFailed is called.
onLoading
The onLoading parameter is called when the tree data is loading. This gives us the opportunity to display a loading signal.
Callback looks like this:
function (isLoading,node,$el)
isLoading: boolean
true: data is loading
false: data is loaded
node:
Node: if a node is loading
null: if the tree is loading
$el:
if a node is loading this is the li element
if the tree is loading is the ul element of the whole tree
openedIcon
A character or symbol to display on opened nodes. The default is ‘▼’ (▼)
The value can be a:
string. E.g. a unicode character or a text.
The text is escaped.
html element. E.g. for an icon
JQuery element. Also for an icon
openFolderDelay
Set the delay for opening a folder during drag-and-drop. The delay is in milliseconds. The default is 500 ms.
Setting the option to false disables opening folders during drag-and-drop.
rtl
Set right-to-left support (true / false). Default is false.
You can also set the option using data-rtl.
saveState
Save and restore the state of the tree automatically. The state is saved in localstorage.
For this to work, you should give each node in the tree data an id field:
true: save and restore state in localstorage
false (default): do nothing
string: save state and use this name to store
Example: save state in key ‘tree1’:
selectable
Turn on selection of nodes.
true (default): turn on selection of nodes
false: turn off selection of nodes
Example: turn off selection of nodes.
showEmptyFolder
true: A node with empty children is considered a folder. Meaning: the node has a ‘children’ attribute, but it’s an empty array.
Show folder icon
Folder can be opened and closed
false (default): A node with empty children is considered a child node
Example with option true:
slide
Turn slide animation on or off. Default is true.
startDndDelay
Sets the delay before drag-and-drop is started. The default is 300 milliseconds.
tabIndex
Set the tabindex of the tree. The default is 0.
Note that the tabindex is set to the selected node.
useContextMenu
Bind the context menu event (true/false).
true (default)
A right mouse-click will trigger a tree.contextmenu event. This overrides the native contextmenu of the browser.
false
A right mouse-click will trigger the native contextmenu of the browser.
Functions
addParentNode
function addParentNode(newNodeInfo, existingNode);
Add a new node as parent of this existing node.
addNodeAfter
function addNodeAfter(newNodeInfo, existingNode);
Add a new node after this existing node.
addNodeBefore
function addNodeBefore(newNodeInfo, existingNode);
Add a new node before this existing node.
appendNode
function appendNode(newNodeInfo, parentNode);
Add a node to this parent node. If parentNode is empty, then the new node becomes a root node.
To add a root node, leave parent_node empty:
It’s also possible to append a subtree:
closeNode
function closeNode(node);
function closeNode(node, slide);
Close this node. The node must have child nodes.
Parameter slide: close the node using a slide animation (default is true).
To close the node without the slide animation, call with slide parameter is false.
destroy
function destroy();
Destroy the tree. This removes the dom elements and event bindings.
getNodeByCallback
function getNodeByCallback(callback);
Get a tree node using a callback. The callback should return true if the node is found.
getNodeById
function getNodeById(id);
Get a tree node by node-id. This assumes that you have given the nodes in the data a unique id.
getNodeByHtmlElement
function getNodeByHtmlElement(htmlElement);
Get a tree node by an html element. The html htmlElement should be:
The li element for the node
Or, an element inside the li. For example the span for the title.
The element can also be a jquery element:
getSelectedNode
Get the selected node. Returns the row data or false.
getState
Get the state of tree: which nodes are open and which one is selected?
Returns a javascript object that contains the ids of open nodes and selected nodes:
If you want to use this function, then your tree data should include an id property for each node.
You can use this function in combination with setState to save and restore the tree state.
getTree
function getTree();
Get the root node of the tree.
isDragging
function isDragging();
Is currently a node being dragged for drag-and-drop? Returns True or False.
isNodeSelected
Return if this node is selected.
loadData
function loadData(data);
function loadData(data, parentNode);
Load data in the tree. The data is array of nodes.
You can replace the whole tree or you can load a subtree.
Load a subtree:
loadDataFromUrl
function loadDataFromUrl(url);
function loadDataFromUrl(url, parentNode);
function loadDataFromUrl(parentNode);
Load data in the tree from an url using ajax. You can replace the whole tree or you can load a subtree.
Load a subtree:
You can also omit the url. In this case jqTree will generate a url for you. This is very useful if you use the load-on-demand feature:
You can also add an on_finished callback parameter that will be called when the data is loaded:
function loadDataFromUrl(url, parentNode, onFinished);
function loadDataFromUrl(parentNode, onFinished);
moveDown
function moveDown()
Select the next node. This does the same as the down key.
moveNode
function moveNode(node, targetNode, position);
Move a node. Position can be ‘before’, ‘after’ or ‘inside’.
moveUp
function moveUp()
Select the previous node. This does the same as the up key.
openNode
function openNode(node);
function openNode(node, slide);
function openNode(node, onFinished);
function openNode(node, slide, onFinished);
Open this node. The node must have child nodes.
Parameter slide (optional): open the node using a slide animation (default is true).
Parameter onFinished (optional): callback when the node is opened; this also works for nodes that are loaded lazily
To open the node without the slide animation, call with slide parameter is false.
Example with on_finished callback:
prependNode
function prependNode(newNodeInfo, parentNode);
Add a node to this parent node as the first child. If parentNode is empty, then the new node becomes a root node.
refresh
function refresh();
Refresh the rendered nodes. In most cases you will not use this, because tree functions will rerender automatically. E.g. The functions openNode and updateNode rerender automatically.
reload
function reload();
function reload(onFinished);
Reload data from the server.
Call onFinished when the data is loaded.
removeNode
function removeNode(node);
Remove node from the tree.
selectNode
function selectNode(node);
function selectNode(null);
function selectNode(node, { mustToggle, mustSetFocus });
Select this node.
You can deselect the current node by calling selectNode(null).
Options
mustSetFocus:
true (default): set the focus to the node; only do this on selection, not deselection
false: do not set the focus
mustToggle:
true (default): toggle; deselected if selected and vice versa
false: select the node, never deselect
scrollToNode
function scrollToNode(node);
Scroll to this node. This is useful if the tree is in a container div and is scrollable.
setOption
function setOption(option, value);
Set a tree option. These are the same options that you can set when creating the tree.
setState
Set the state of the tree: which nodes are open and which one is selected?
Update the title of a node. You can also update the data.
Update the name:
Update the data (including the name)
It is also possible to update the children. Note that this removes the existing children:
Events
tree.click
Triggered when a tree node is clicked. The event contains the following properties:
node: the node that is clicked on
click_event: the original click event
The default action is to select the node. You can prevent the selection by calling preventDefault:
tree.close
Called when a node is closed.
tree.contextmenu
Triggered when the user right-clicks a tree node. The event contains the following properties:
node: the node that is clicked on
click_event: the original click event
tree.dblclick
The tree.dblclick is fired when a tree node is double-clicked. The event contains the following properties:
node: the node that is clicked on
click_event: the original click event
tree.init
Called when the tree is initialized. This is particularly useful when the data is loaded from the server.
tree.load_data
Called after data is loaded using ajax.
tree.loading_data
Called before and after data is loaded using ajax.
The event data looks like this:
isLoading: true / false
node:
null; when loading the whole tree
a node; when a node is loaded on demand
$el: dom element
whole tree; when loading the whole tree
dom element of node; when a node is loaded on demand
Example code:
tree.move
Triggered when the user moves a node.
Note that this event is called before the node is moved. See note about do_move below.
Event.move_info contains:
moved_node
target_node
position: (before, after or inside)
previous_parent
You can prevent the move by calling event.preventDefault()
You can later call event.move_info.move_info.do_move() to move the node. This way you can ask the user before moving the node:
Note that if you want to serialise the tree, for example to POST back to a server, you need to let tree complete the move first:
tree.refresh
The tree.refresh event is triggered when the tree is repainted.
Examples when the tree.refresh event is fired:
after the first draw of the tree
after a node is moved
after the updateNode method is called
tree.open
Called when a node is opened.
tree.select
Triggered when a tree node is selected or deselected.
If a node is selected, then event.node contains the selected node.
If a node is deselected, then the event.node property is null.
Multiple selection
Jqtree has some functions that can help you to implement multiple selection. See Example 8 - multiple select.
In order for multiple selection to work, you must give the nodes an id.
addToSelection
Add this node to the selection. Also set the focus to the node.
function addToSelection(node, mustSetFocus = true);
Parameter mustSetFocus: set the focus to the node (default true).
Without setting the focus:
getSelectedNodes
Get the selected nodes. Return an array of nodes
removeFromSelection
Remove this node from the selection.
Node functions
You can access a node using for example getNodeById function:
The Node object has the following properties and functions:
children
You can access the children of a node using the children property.
getData
function getData(includeParent = false);
Get the subtree of this node.
includeParent
true: include node and children
false: only include children (default)
getLevel
Get the level of a node. The level is distance of a node to the root node.
getNextNode
Get the next node in the tree. This is the next sibling, if there is one. Or, if there is no next sibling, a node further down in the tree.
Returns a node or null.
getNextSibling
Get the next sibling of this node. Returns a node or null.
getNextVisibleNode
Get the next visible node in the tree. Does the same as using the down key.
This is the previous sibling, if there is one. Or, if there is no previous sibling, a node further up in the tree that is visible.
Returns a node or null.
A node is visible if all its parents are open.
getPreviousNode
Return the previous node in the tree. This is the previous sibling, if there is one. Or, if there is no previous sibling, a node further up in the tree.
Returns a node or null.
getPreviousSibling
Get the previous sibling of this node. Returns a node or null.
getPreviousVisibleNode
Get the previous visible node in the tree. Does the same as using the up key.
This is the previous sibling, if there is one. Or, if there is no previous sibling, a node further up in the tree that is visible.
Returns a node or null.
A node is visible if all its parents are open.
parent
You can access the parent of a node using the parent property.