Introducing PrimeReact v11-alpha 🎉Discover Now
styledData

Tree

Tree is used to display hierarchical data.

    preview

    Usage#

    import { Tree } from '@primereact/ui/tree';
    <Tree.Root>
        <Tree.Header />
        <Tree.List>
            <Tree.Node>
                <Tree.Content>
                    <Tree.Toggle />
                </Tree.Content>
            </Tree.Node>
        </Tree.List>
        <Tree.Footer />
        <Tree.Empty />
    </Tree.Root>

    Examples#

    Basic#

    Displays hierarchical data with expand and collapse support.

    basic-demo

    Node#

    Tree nodes can be customized with templates using the Tree.Node components.

      node-demo

      Controlled#

      Tree state can be controlled programmatically with the expandedKeys property that defines the keys that are expanded. This property is a Map instance whose key is the key of a node and value is a boolean.

      • app
      controlled-demo

      Selection#

      Single#

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

      single-selection-demo

      Multiple#

      More than one node is selectable by setting selectionMode to multiple. By default in multiple selection mode, metaKey press (e.g. ⌘) is not necessary to add to existing selections. When the optional metaKeySelection is present, behavior is changed in a way that selecting a new node requires meta key to be present. 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.

      multiple-selection-demo

      Checkbox#

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

      In checkbox selection mode, value binding should be a key-value pair where key is the node key and value is an object that has checked and partialChecked properties to represent the checked state of a node object to indicate selection.

        checkbox-selection-demo

        Lazy Loading#

        Lazy loading is demonstrated in this example where nodes are loaded on demand when a node is expanded.

          lazy-demo

          Filter#

          Filtering enables searching through the nodes. Place a Tree.Filter component inside Tree.Header to add a search input. Any input component can be used with the as prop and the filtering logic can be controlled with the onChange event.

          No options found.
          filter-demo

          DragDrop#

          Single#

          Drag&Drop based reordering is enabled by adding the draggableNodes and droppableNodes properties..

          drag-drop-single-demo

          Multiple#

          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.

          Drag Nodes Here
          drag-drop-multiple-demo

          API#

          Sub-Components#

          See Primitive API for TreeRoot, TreeList, TreeNode, TreeContent, TreeToggle, TreeIcon, TreeLabel, TreeFilter, TreeHeader, TreeFooter, TreeEmpty component documentation.

          Hooks#

          See Headless API for useTree and useTreeNode hook documentation.

          Accessibility#

          See Tree Primitive for WAI-ARIA compliance details and keyboard support.