Skip to content

Node ​

Every node in the tree is a Node object. You get them from the tree — getNodeById, getSelectedNode, an event's detail.node — and they are what you pass back into the tree's methods.

js
const node = tree.getNodeById(1);

node.name; // "node1"
node.isFolder(); // true
node.getLevel(); // 1

The root node is not rendered; it is a container for the top-level nodes:

js
tree.getTree().children; // the top-level nodes

Properties ​

Any other key in the node data is copied onto the node, so { name: "node1", color: "green" } gives you node.color.

PropertyTypeDefault valueDescription
childrenNode[]undefinedThe child nodes.
element?HTMLElementundefinedThe li element, once the node is rendered. Nodes inside a closed folder are rendered when the folder is opened.
id?NodeIdundefinedThe id from the node data.
is_loading?booleanundefinedWhether the node's children are being fetched.
is_open?booleanundefinedWhether the folder is open.
isEmptyFolderbooleanundefinedWhether the node data had an empty children array.
load_on_demandbooleanfalseWhether the children still have to be fetched.
namestring""The label. Also settable from the label key in node data.
parentNode | nullundefinedThe parent; null for the root node.
tree?NodeundefinedThe root node.

Inspecting a node ​

getChildIndex() ​

ts
getChildIndex(node): number;

Returns the position of a child in children, or -1.

Parameters ​

ParameterType
nodeNode

Returns ​

number


getLevel() ​

ts
getLevel(): number;

Returns the depth of the node, counting the top level as 1.

Returns ​

number


hasChildren() ​

ts
hasChildren(): boolean;

Whether the node has children.

Returns ​

boolean


isFolder() ​

ts
isFolder(): boolean;

true when the node has children, or is marked load_on_demand.

Returns ​

boolean


isParentOf() ​

ts
isParentOf(node): boolean;

Whether this node is an ancestor of the other node.

Parameters ​

ParameterType
nodeNode

Returns ​

boolean

Moving around the tree ​

getLastChild() ​

ts
getLastChild(): Node | null;

Returns the last child, or null. When that child is an open folder, its own last child, and so on.

Returns ​

Node | null


getNextNode() ​

ts
getNextNode(includeChildren?): Node | null;

Returns the next node in the tree, regardless of whether it is visible. getNextNode(false) skips the node's own children.

Parameters ​

ParameterTypeDefault value
includeChildren?booleantrue

Returns ​

Node | null


getNextSibling() ​

ts
getNextSibling(): Node | null;

Returns the next sibling, or null.

Returns ​

Node | null


getNextVisibleNode() ​

ts
getNextVisibleNode(): Node | null;

Like getNextNode, but skipping nodes inside closed folders — this is what the arrow keys use.

Returns ​

Node | null


getParent() ​

ts
getParent(): Node | null;

Returns the parent: null for a top-level node — the root node is not returned.

Returns ​

Node | null


getPreviousNode() ​

ts
getPreviousNode(): Node | null;

Returns the previous node in the tree, regardless of whether it is visible.

Returns ​

Node | null


getPreviousSibling() ​

ts
getPreviousSibling(): Node | null;

Returns the previous sibling, or null.

Returns ​

Node | null


getPreviousVisibleNode() ​

ts
getPreviousVisibleNode(): Node | null;

Like getPreviousNode, but skipping nodes inside closed folders — this is what the arrow keys use.

Returns ​

Node | null

Searching ​

These work on any node, and search that node's subtree. Called on the root node, they search the whole tree — which is what the tree's own methods of the same name do.

filter() ​

ts
filter(f): Node[];

Returns all nodes in the subtree for which the callback returns true.

Parameters ​

ParameterType
f(node) => boolean

Returns ​

Node[]

Example ​

js
const folders = tree.getTree().filter((node) => node.isFolder());

getNodeByCallback() ​

ts
getNodeByCallback(callback): Node | null;

Returns the first node for which the callback returns true.

Parameters ​

ParameterType
callback(node) => boolean

Returns ​

Node | null


getNodeById() ​

ts
getNodeById(nodeId): Node | null;

Returns the node with this id. Only available on the root node, which keeps the id index.

Parameters ​

ParameterType
nodeIdNodeId

Returns ​

Node | null


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.

Parameters ​

ParameterType
namestring

Returns ​

Node


getNodesByProperty() ​

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

Returns all nodes with this property value.

Parameters ​

ParameterType
keystring
valueunknown

Returns ​

Node[]


iterate() ​

ts
iterate(callback): void;

Walks the subtree, calling the callback with (node, level). Return false from the callback to stop descending into that node:

js
tree.getTree().iterate((node, level) => {
  console.log(" ".repeat(level) + node.name);
  return level <= 2; // don't go deeper than level 2
});

Parameters ​

ParameterType
callbackIterateCallback

Returns ​

void

Changing the tree ​

WARNING

These methods change the data without re-rendering. The tree's methods — appendNode, removeNode, moveNode and friends — do the same and refresh the display, so prefer those. If you do use these, call tree.refresh() afterwards.

addAfter() ​

ts
addAfter(nodeInfo): Node | null;

Adds a sibling after this node.

Parameters ​

ParameterType
nodeInfoNodeData

Returns ​

Node | null

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


addBefore() ​

ts
addBefore(nodeInfo): Node | null;

Adds a sibling before this node.

Parameters ​

ParameterType
nodeInfoNodeData

Returns ​

Node | null

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


addChild() ​

ts
addChild(node): void;

Adds an existing Node object as the last child.

Parameters ​

ParameterType
nodeNode

Returns ​

void


addChildAtPosition() ​

ts
addChildAtPosition(node, index): void;

Adds an existing Node object as a child, at this position in children. The index starts at 0.

Parameters ​

ParameterType
nodeNode
indexnumber

Returns ​

void


addParent() ​

ts
addParent(nodeInfo): Node | null;

Inserts a new parent between this node and its current parent.

Parameters ​

ParameterType
nodeInfoNodeData

Returns ​

Node | null

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


append() ​

ts
append(nodeInfo): Node;

Adds a child at the end.

Parameters ​

ParameterType
nodeInfoNodeData

Returns ​

Node

The new node.


loadFromData() ​

ts
loadFromData(data): this;

Replaces the children with new node data.

Parameters ​

ParameterType
dataNodeData[]

Returns ​

this


moveNode() ​

ts
moveNode(
   movedNode, 
   targetNode, 
   position
): boolean;

Moves a node relative to another node. Called on the root node.

Parameters ​

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

Returns ​

boolean

false when the move is impossible, for instance moving a node into its own subtree.


prepend() ​

ts
prepend(nodeInfo): Node;

Adds a child at the start.

Parameters ​

ParameterType
nodeInfoNodeData

Returns ​

Node

The new node.


remove() ​

ts
remove(): void;

Removes this node from its parent.

Returns ​

void


removeChild() ​

ts
removeChild(node): void;

Removes a child and its children.

Parameters ​

ParameterType
nodeNode

Returns ​

void


removeChildren() ​

ts
removeChildren(): void;

Removes all children.

Returns ​

void


setData() ​

ts
setData(o): void;

Updates the node's properties from node data. children and parent are ignored. A string sets the name. Existing node values are not removed.

Parameters ​

ParameterType
oNodeData | null

Returns ​

void

Reading data back ​

getData() ​

ts
getData(includeParent?): NodeRecord[];

Returns the subtree as plain data, ready for JSON.stringify. Internal properties (parent, children, element, tree, idMapping, nodeClass, load_on_demand, isEmptyFolder) are left out; your own properties are kept.

js
tree.getTree().getData();
// [{ name: "node1", id: 1, children: [{ name: "child1", id: 2 }] }]

Parameters ​

ParameterTypeDefault valueDescription
includeParent?booleanfalseMake the result the node itself rather than its children. Default false.

Returns ​

NodeRecord[]