Tree

Tree is used to display hierarchical data.

import { TreeModule } from 'primeng/tree';

Tree component requires an array of TreeNode objects as its value.

  • Documents
  • Events
  • Movies

Tree requires a collection of TreeNode instances as a value.

  • Documents
  • Events
  • Movies

Single node selection is configured by setting selectionMode as single along with selection properties to manage the selection value binding.

  • Documents
  • Events
  • Movies

More than one node is selectable by setting selectionMode to multiple. By default in multiple selection mode, metaKey press (e.g. ⌘) is necessary to add to existing selections however this can be configured with disabling the metaKeySelection property. Note that in touch enabled devices, Tree always ignores metaKey.

In multiple selection mode, value binding should be a key-value pair where key is the node key and value is a boolean to indicate selection.

  • Documents
  • Events
  • Movies

Selection of multiple nodes via checkboxes is enabled by configuring selectionMode as checkbox.

  • Documents
  • Events
  • Movies

An event is provided for each type of user interaction such as expand, collapse and selection.

  • Documents
  • Events
  • Movies

Lazy loading is useful when dealing with huge datasets, in this example nodes are dynamically loaded on demand using loading property and onNodeExpand method.

  • Node 0
  • Node 1
  • Node 2

Custom node content instead of a node label is defined with the pTemplate property.

  • Introduction
  • Components In-Depth

Filtering is enabled by adding the filter property, by default label property of a node is used to compare against the value in the text field, in order to customize which field(s) should be used during search define filterBy property. In addition filterMode specifies the filtering strategy. In lenient mode when the query matches a node, children of the node are not searched further as all descendants of the node are included. On the other hand, in strict mode when the query matches a node, filtering continues on all descendants.

  • Documents
  • Events
  • Movies
  • Documents
  • Events
  • Movies

VirtualScroller is a performance-approach to handle huge data efficiently. Setting virtualScroll property as true and providing a virtualScrollItemSize in pixels would be enough to enable this functionality.

VirtualScroller is a performance-approach to handle huge data efficiently. Setting virtualScroll property as true and providing a virtualScrollItemSize in pixels would be enough to enable this functionality.

Nodes can be reordered within the same tree and also can be transferred between other trees using drag&drop.

  • .github
  • .vscode
  • public
  • src
  • index.html
  • package.json
  • vite.config.js

Nodes can be transferred between multiple trees as well. The draggableScope and droppableScope properties defines keys to restrict the actions between trees. In this example, nodes can only be transferred from start to the end.

  • .github
  • .vscode
  • public
  • src
  • index.html
  • package.json
  • vite.config.js
  • /etc
Drag Nodes Here

Tree has exclusive integration with ContextMenu using the contextMenu property along with the contextMenuSelection to manage the selection.

  • Documents
  • Events
  • Movies

Screen Reader

Value to describe the component can either be provided with aria-labelledby or aria-label props. The root list element has a tree role whereas each list item has a treeitem role along with aria-label, aria-selected and aria-expanded attributes. In checkbox selection, aria-checked is used instead of aria-selected. The container element of a treenode has the group role. Checkbox and toggle icons are hidden from screen readers as their parent element with treeitem role and attributes are used instead for readers and keyboard support. The aria-setsize, aria-posinset and aria-level attributes are calculated implicitly and added to each treeitem.

Keyboard Support

KeyFunction
tab Moves focus to the first selected node when focus enters the component, if there is none then first element receives the focus. If focus is already inside the component, moves focus to the next focusable element in the page tab sequence.
shift + tab Moves focus to the last selected node when focus enters the component, if there is none then first element receives the focus. If focus is already inside the component, moves focus to the previous focusable element in the page tab sequence.
enterSelects the focused treenode.
spaceSelects the focused treenode.
down arrowMoves focus to the next treenode.
up arrowMoves focus to the previous treenode.
right arrowIf node is closed, opens the node otherwise moves focus to the first child node.
left arrowIf node is open, closes the node otherwise moves focus to the parent node.
PrimeBlocks
490+ ready to use UI blocks crafted with PrimeNG and Tailwind CSS.