Tree is used to display hierarchical data.
import { TreeModule } from 'primeng/tree';
Tree component requires an array of TreeNode objects as its value.
<p-tree class="w-full md:w-30rem" [value]="files" />
Tree requires a collection of TreeNode instances as a value.
<div class="mb-3">
<p-button
icon="pi pi-plus"
label="Expand all"
(onClick)="expandAll()"
class="mr-2" />
<p-button
icon="pi pi-minus"
label="Collapse all"
(onClick)="collapseAll()" />
</div>
<p-tree [value]="files" class="w-full md:w-30rem" />
Single node selection is configured by setting selectionMode as single along with selection properties to manage the selection value binding.
<p-tree
[value]="files"
class="w-full md:w-30rem"
selectionMode="single"
[(selection)]="selectedFile" />
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.
<div class="flex align-items-center mb-4 gap-2">
<p-inputSwitch inputId="input-metakey" [(ngModel)]="metaKeySelection" />
<label for="input-metakey">MetaKey</label>
</div>
<p-tree
[metaKeySelection]="metaKeySelection"
[value]="files"
class="w-full md:w-30rem"
selectionMode="multiple"
[(selection)]="selectedFiles" />
Selection of multiple nodes via checkboxes is enabled by configuring selectionMode as checkbox.
<p-tree
[value]="files"
selectionMode="checkbox"
class="w-full md:w-30rem"
[(selection)]="selectedFiles" />
An event is provided for each type of user interaction such as expand, collapse and selection.
<p-tree
[value]="files"
class="w-full md:w-30rem"
selectionMode="single"
[(selection)]="selectedFile"
(onNodeExpand)="nodeExpand($event)"
(onNodeCollapse)="nodeCollapse($event)"
(onNodeSelect)="nodeSelect($event)"
(onNodeUnselect)="nodeUnselect($event)" />
Lazy loading is useful when dealing with huge datasets, in this example nodes are dynamically loaded on demand using loading property and onNodeExpand method. Default value of loadingMode is mask and also icon is available.
<p-tree
class="w-full md:w-30rem"
[value]="nodes"
(onNodeExpand)="onNodeExpand($event)"
[loading]="loading" />
<p-tree
class="w-full md:w-30rem"
[value]="nodes2"
loadingMode="icon"
(onNodeExpand)="onNodeExpand2($event)" />
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.
<p-tree
class="w-full md:w-30rem"
scrollHeight="250px"
[virtualScroll]="true"
[virtualScrollItemSize]="46"
[value]="files" />
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.
<p-tree
class="w-full md:w-30rem"
scrollHeight="250px"
[virtualScroll]="true"
[lazy]="true"
[virtualScrollItemSize]="46"
[value]="files"
(onNodeExpand)="nodeExpand($event)"
[loading]="loading" />
Custom node content instead of a node label is defined with the pTemplate property.
<p-tree [value]="nodes" class="w-full md:w-30rem">
<ng-template let-node pTemplate="url">
<a [href]="node.data" target="_blank" rel="noopener noreferrer" class="text-700 hover:text-primary">
{{ node.label }}
</a>
</ng-template>
<ng-template let-node pTemplate="default">
<b>{{ node.label }}</b>
</ng-template>
</p-tree>
Nodes can be reordered within the same tree and also can be transferred between other trees using drag&drop.
<p-tree
class="w-full md:w-30rem"
[value]="files"
[draggableNodes]="true"
[droppableNodes]="true"
draggableScope="self"
droppableScope="self" />
Tree requires a collection of TreeNode instances as a value.
<p-tree
class="w-full md:w-30rem"
[value]="files"
selectionMode="single"
[(selection)]="selectedFile"
[contextMenu]="cm" />
<p-contextMenu #cm [model]="items" />
<p-toast />
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.
<p-tree
[value]="files"
class="w-full md:w-30rem"
[filter]="true"
filterPlaceholder="Lenient Filter" />
<p-tree
[value]="files"
class="w-full md:w-30rem"
[filter]="true"
filterMode="strict"
filterPlaceholder="Strict Filter" />
Following is the list of structural style classes, for theming classes visit theming page.
Name | Element |
---|---|
p-tree | Main container element |
p-tree-horizontal | Main container element in horizontal mode |
p-tree-container | Container of nodes |
p-treenode | A treenode element |
p-treenode-content | Content of a treenode |
p-treenode-toggler | Toggle icon |
p-treenode-icon | Icon of a treenode |
p-treenode-label | Label of a treenode |
p-treenode-children | Container element for node children |
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.
Key | Function |
---|---|
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. |
enter | Selects the focused treenode. |
space | Selects the focused treenode. |
down arrow | Moves focus to the next treenode. |
up arrow | Moves focus to the previous treenode. |
right arrow | If node is closed, opens the node otherwise moves focus to the first child node. |
left arrow | If node is open, closes the node otherwise moves focus to the parent node. |
API defines helper props, events and others for the PrimeNG Tree module.
Tree is used to display hierarchical data.
Defines the input properties of the component.
Defines emit that determine the behavior of the component based on a given condition or report the actions that the component takes.
name | parameters | description | |
---|---|---|---|
selectionChange | value : TreeNode | Callback to invoke on selection change. | |
onNodeSelect | event : TreeNodeSelectEvent | Callback to invoke when a node is selected. | |
onNodeUnselect | event : TreeNodeUnSelectEvent | Callback to invoke when a node is unselected. | |
onNodeExpand | event : TreeNodeExpandEvent | Callback to invoke when a node is expanded. | |
onNodeCollapse | event : TreeNodeCollapseEvent | Callback to invoke when a node is collapsed. | |
onNodeContextMenuSelect | event : TreeNodeContextMenuSelectEvent | Callback to invoke when a node is selected with right click. | |
onNodeDrop | event : TreeNodeDropEvent | Callback to invoke when a node is dropped. | |
onLazyLoad | event : TreeLazyLoadEvent | Callback to invoke in lazy mode to load new data. | |
onScroll | event : TreeScrollEvent | Callback to invoke in virtual scroll mode when scroll position changes. | |
onScrollIndexChange | event : TreeScrollIndexChangeEvent | Callback to invoke in virtual scroll mode when scroll position and item's range in view changes. | |
onFilter | event : TreeFilterEvent | Callback to invoke when data is filtered. |
Defines methods that can be accessed by the component's reference.
Defines the templates used by the component.
Defines the custom events used by the component's emitters.
Custom node select event.
Custom node unselect event.
Custom node expand event.
Custom node collapse event.
Custom context menu select event.
Custom node drop event.
Custom lazy load event.
Custom scroll index change event.
Custom scroll event.
Custom filter event.
Defines the custom interfaces used by the module.
Represents a node in a tree data structure.