Skip to content

Methods ​

Methods are called on the object returned by the constructor:

js
const tree = new TreeElement({ data, htmlElement });

tree.openNode(tree.getNodeById(1));

Select a node and try them here — the buttons call appendNode, updateNode, removeNode, openNode and closeNode:

Events

Click a node, open a folder, or drag something.

Finding nodes ​

getNode() ​

ts
getNode(element): Node | null;

Returns the node that belongs to a li element the tree rendered.

Parameters ​

ParameterType
elementHTMLElement

Returns ​

Node | null


getNodeByCallback() ​

ts
getNodeByCallback(callback): Node | null;

Returns the first node for which the callback returns true.

Parameters ​

ParameterType
callback(node) => boolean

Returns ​

Node | null

Example ​

js
const node = tree.getNodeByCallback((node) => node.children.length > 3);

getNodeById() ​

ts
getNodeById(id): Node | null;

Returns the node with this id.

Parameters ​

ParameterType
idNodeId

Returns ​

Node | null

Example ​

js
const node = tree.getNodeById(1);

getNodeByName() ​

ts
getNodeByName(name): Node | null;

Returns the first node with this name.

Parameters ​

ParameterType
namestring

Returns ​

Node | null


getNodeByNameMustExist() ​

ts
getNodeByNameMustExist(name): Node;

Like getNodeByName, but throws when there is no such node. Convenient in tests and when a missing node is a bug.

Parameters ​

ParameterType
namestring

Returns ​

Node


getNodesByProperty() ​

ts
getNodesByProperty(key, value): Node[];

Returns all nodes with this property value.

Parameters ​

ParameterType
keystring
valueunknown

Returns ​

Node[]

Example ​

js
tree.getNodesByProperty("color", "green");

getTree() ​

ts
getTree(): Node;

Returns the root node. It is not rendered; its children are the top-level nodes. Also available as the tree property.

Returns ​

Node

Opening and closing ​

closeNode() ​

ts
closeNode(node, slide?): Promise<void>;

Closes a folder. Await the promise when you need to know the closing animation has finished.

Parameters ​

ParameterTypeDescription
nodeNode-
slide?booleanOverride the slide option for this call.

Returns ​

Promise<void>

Example ​

js
await tree.closeNode(node);
console.log("closed");

openNode() ​

ts
openNode(node, slide?): Promise<void>;

Opens a folder, and the folders above it. A node marked load_on_demand is fetched first, so await the promise when you need to know it is really open.

Parameters ​

ParameterTypeDescription
nodeNode-
slide?booleanOverride the slide option for this call.

Returns ​

Promise<void>

Example ​

js
await tree.openNode(node);
console.log("open");

toggle() ​

ts
toggle(node, slide?): Promise<void>;

Closes an open node and opens a closed one. Await the promise when you need to know the node is really open or closed, like with openNode and closeNode.

Parameters ​

ParameterTypeDefault valueDescription
nodeNodeundefined-
slide?boolean | nullnullOverride the slide option for this call.

Returns ​

Promise<void>

Selection ​

addToSelection() ​

ts
addToSelection(node, mustSetFocus?): void;

Adds a node to the selection instead of replacing it.

Parameters ​

ParameterTypeDefault valueDescription
nodeNodeundefined-
mustSetFocus?booleantrueMove the focus to the node. Default true.

Returns ​

void


getSelectedNode() ​

ts
getSelectedNode(): Node | null;

Returns the selected node, or null when nothing is selected.

Returns ​

Node | null


getSelectedNodes() ​

ts
getSelectedNodes(): Node[];

Returns the selected nodes.

Returns ​

Node[]


isNodeSelected() ​

ts
isNodeSelected(node): boolean;

Returns whether the node is selected.

Parameters ​

ParameterType
nodeNode

Returns ​

boolean


moveDown() ​

ts
moveDown(): void;

Selects the next visible node, like the down arrow key. With multiSelect it moves the focus instead.

Returns ​

void


moveUp() ​

ts
moveUp(): void;

Selects the previous visible node, like the up arrow key. With multiSelect it moves the focus instead.

Returns ​

void


removeFromSelection() ​

ts
removeFromSelection(node): void;

Removes a node from the selection.

Parameters ​

ParameterType
nodeNode

Returns ​

void


scrollToNode() ​

ts
scrollToNode(node): void;

Scrolls the node into view.

Parameters ​

ParameterType
nodeNode

Returns ​

void


selectNode() ​

ts
selectNode(node, options?): void;

Replaces the selection, and opens the parents of the node.

Parameters ​

ParameterTypeDescription
nodeNode | nullnull clears the selection.
options?SelectNodeOptionsmustSetFocus: move focus to the node, default true. mustToggle: deselect the node if it is already selected, default false.

Returns ​

void

Changing the tree ​

addNodeAfter() ​

ts
addNodeAfter(nodeData, existingNode): Node | null;

Adds a sibling after a node.

Parameters ​

ParameterType
nodeDataNodeData
existingNodeNode

Returns ​

Node | null

The new node, or null when the node has no parent.


addNodeBefore() ​

ts
addNodeBefore(nodeData, existingNode): Node | null;

Adds a sibling before a node.

Parameters ​

ParameterType
nodeDataNodeData
existingNodeNode

Returns ​

Node | null

The new node, or null when the node has no parent.


addParentNode() ​

ts
addParentNode(nodeData, existingNode): Node | null;

Inserts a new node between a node and its parent, taking the node as its child.

Parameters ​

ParameterType
nodeDataNodeData
existingNodeNode

Returns ​

Node | null

The new node, or null when the node has no parent.


appendNode() ​

ts
appendNode(nodeData, parentNode): Node;

Adds a node as the last child of a parent.

Parameters ​

ParameterType
nodeDataNodeData
parentNodeNode

Returns ​

Node

The new node.

Example ​

js
tree.appendNode({ name: "child", id: 5 }, tree.getNodeById(1));

moveNode() ​

ts
moveNode(
   node, 
   targetNode, 
   position
): void;

Moves a node inside the tree.

Parameters ​

ParameterTypeDescription
nodeNode-
targetNodeNode-
positionPosition"before", "after" or "inside".

Returns ​

void

Example ​

js
tree.moveNode(tree.getNodeById(2), tree.getNodeById(1), "inside");

prependNode() ​

ts
prependNode(nodeData, parentNode): Node;

Adds a node as the first child of a parent.

Parameters ​

ParameterType
nodeDataNodeData
parentNodeNode

Returns ​

Node

The new node.


refresh() ​

ts
refresh(): void;

Re-renders the whole tree. Needed after changing node data directly instead of through these methods.

Returns ​

void


removeNode() ​

ts
removeNode(node): void;

Removes a node and its children.

Parameters ​

ParameterType
nodeNode

Returns ​

void


updateNode() ​

ts
updateNode(node, data): void;

Updates the data of a node and re-renders it. A string updates just the name.

Changing the id re-indexes the node. children and parent are ignored — use loadData or moveNode for those.

Parameters ​

ParameterType
nodeNode
dataNodeData

Returns ​

void

Example ​

js
tree.updateNode(node, "new name");
tree.updateNode(node, { name: "new name", color: "red" });

Loading data ​

loadData() ​

ts
loadData(data, parentNode?): void;

Loads data into the tree.

Parameters ​

ParameterTypeDescription
dataNodeData[] | null-
parentNode?NodeReplace this node's children instead of the whole tree.

Returns ​

void


loadDataFromUrl() ​

ts
loadDataFromUrl(url?, parentNode?): Promise<void>;

Fetches data and loads it into the tree, or into parentNode.

Parameters ​

ParameterTypeDescription
url?stringDefaults to the dataUrl option.
parentNode?Node-

Returns ​

Promise<void>

State ​

getState() ​

ts
getState(): SavedState | null;

Returns the current state — open_nodes and selected_node — whether or not saveState is enabled.

Returns ​

SavedState | null


getStateFromStorage() ​

ts
getStateFromStorage(): SavedState | null;

Returns the state as it is stored.

Returns ​

SavedState | null


setState() ​

ts
setState(state): void;

Applies a state to the tree, and saves it to storage when saveState is enabled.

Parameters ​

ParameterType
stateSavedState

Returns ​

void

Other ​

deinit() ​

ts
deinit(): void;

Empties the element and removes the tree's document-level keyboard listener. Call it when you remove the tree from the page.

Returns ​

void


getVersion() ​

ts
getVersion(): string;

Returns the version of tree-element.

Returns ​

string


isDragging() ​

ts
isDragging(): boolean;

Returns whether the user is dragging a node.

Returns ​

boolean


refreshHitAreas() ​

ts
refreshHitAreas(): void;

Recomputes the drop targets. Call this if the layout changes during a drag.

Returns ​

void


setOption() ​

ts
setOption(option, value): void;

Changes an option after construction. Only some options can be changed; the others throw an error. See Options.

Parameters ​

ParameterType
option| "animationSpeed" | "dataUrl" | "dragAndDrop" | "keyboardSupport" | "multiSelect" | "onCanSelectNode" | "selectable" | "slide" | "startDndDelay" | "tabIndex"
valueunknown

Returns ​

void


toJson() ​

ts
toJson(): string;

Returns the tree as a json string, including changes made through the api.

Returns ​

string