Tree

Live Demo

Overview

The Tree component allows a natural way to represent data that has hierarchical relationships. The user can drill down in the hierarchy by expanding items by clicking on the expand arrow, and likewise collapse items. Tree is a selection component that allows selecting items.

A typical use of the Tree component is for displaying a hierarchical menu, as illustrated in A Tree component, or for displaying file systems or hierarchical datasets.

tree basic

A Tree component

Binding to Data

Tree is used by binding it to a hierarchical data provider. The data provider can be based on in-memory or back end data. For in-memory data, the TreeDataProvider can be used, and for loading data from a back end, you need to implement three methods from the HierarchicalDataProvider interface. Usage of both data providers is described in “Hierarchical Data”.

The TreeData class can be used to build the hierarchical data structure, and it can then be passed on to TreeDataProvider. It is simply a hierarchical collection, that the data provider uses to populate the Tree.

The setItems method in Tree can be used to set the root level items. Internally an TreeDataProvider with TreeData is used.

Java

  1. // An initial planet tree
  2. Tree<String> tree = new Tree<>();
  3. TreeData<String> treeData = new TreeData<>();
  4. // Couple of childless root items
  5. treeData.addItem(null,"Mercury");
  6. treeData.addItem(null,"Venus");
  7. // Items with hierarchy
  8. treeData.addItem(null,"Earth");
  9. treeData.addItem("Earth","The Moon");
  10. inMemoryDataProvider = new TreeDataProvider<>(treeData);
  11. tree.setDataProvider(inMemoryDataProvider);
  12. tree.expand("Earth"); // Expand programmatically

If at any time you want to modify the in-memory data in the tree, you may do it as follows:

Java

  1. // Add Mars with satellites
  2. treeData.addItem(null, "Mars");
  3. treeData.addItem("Mars", "Phobos");
  4. treeData.addItem("Mars", "Deimos");
  5. inMemoryDataProvider.refreshAll();

The result was shown in A Tree component.

The caption and the icon of tree items is generated by the ItemCaptionGenerator and the IconGenerator, set with setItemCaptionGenerator() and setItemIconGenerator() respectively.

Handling Selection and Clicks

Tree supports single selection mode, you can use asSingleSelect() to access the selection object, which supports selection listeners and data binding. For more details, see link:“Selecting Items”. The Tree also supports the shortcut method addSelectionListener().

Tree also emits ItemClickEvents when items are clicked. This way you can handle item clicks also when you want special user interaction specifically on clicks.

Java

  1. tree.addItemClickListener(event ->
  2. Notification.show("Click",
  3. Notification.Type.HUMANIZED_MESSAGE)
  4. );

Right-clicks

Right-clicks are supported similar way via addContextClickListener() method

Java

  1. tree.addContextClickListener(event -> Notification.show(
  2. ((TreeContextClickEvent<Person>)event).getItem() + " Clicked")
  3. );

Expanding and Collapsing Nodes

Tree nodes that have children can be expanded and collapsed by either user interaction or through the server-side API:

Java

  1. // Expands a child project. If the child project is not yet
  2. // in the visible hierarchy, nothing will be shown.
  3. tree.expand(childProject);
  4. // Expands the root project. If child project now becomes
  5. // visible it is also expanded into view.
  6. tree.expand(rootProject);
  7. // Collapses the child project.
  8. tree.collapse(childProject);

To use the server-side API with a backend data provider the hashCode and equals methods for the node’s type must be implemented so that when the desired node is retrieved from the backend it can be correctly matched with the object passed to either expand or collapse.

The Tree component supports listening to the expansion and collapsing of items in its hierarchy. The expand and collapse listeners can be added as follows:

Java

  1. tree.addExpandListener(event -> log("Item expanded: " + event.getExpandedItem()));
  2. tree.addCollapseListener(event -> log("Item collapsed: " + event.getCollapsedItem()));

The return types of the methods getExpandedItem and getCollapsedItem are the same as the type of the Tree the events originated from. Note that collapse listeners will not be triggered for any expanded subtrees of the collapsed item.

Prevent Node Collapsing

Tree supports setting a callback method that can allow or prevent the user from collapsing an expanded node. It can be set with setItemCollapseAllowedProvider method, that takes a ItemCollapseAllowedProvider. For nodes that cannot be collapsed, the collapse-disabled class name is applied to the expansion element

Avoid doing any heavy operations in the method, since it is called for each item when it is being sent to the client.

Example using a predefined set of persons that can not be collapsed:

Java

  1. Set<Person> alwaysExpanded;
  2. personTree.setItemCollapseAllowedProvider(person ->
  3. !alwaysExpanded.contains(person));

Keyboard Navigation and Focus Handling in TreeGrid

The user can navigate through rows with Up and Down, collapse rows with Left, and expand them with Right.

CSS Style Rules

CSS

  1. .v-tree8 {
  2. .v-tree8-scroller, .v-tree8-scroller-horizontal { }
  3. .v-tree8-tablewrapper {
  4. .v-tree8-body {
  5. .v-tree8-row,
  6. .v-tree8-stripe,
  7. .v-tree8-row-focused,
  8. .v-tree8-row-has-data {
  9. .v-tree8-expander, expanded {}
  10. .v-tree8-cell-content {}
  11. }
  12. }
  13. }
  14. }

Generating Item Styles

You can style each tree item individually by generating a style name for them with a StyleGenerator, which you assign to a tree with setStyleGenerator(). The generator should return a style name for each item or null.

Java

  1. // Show all leaf nodes as disabled
  2. tree.setStyleGenerator(item -> {
  3. if (!tree.getDataProvider().hasChildren(item))
  4. return "leaf";
  5. return null;
  6. }
  7. );

CSS

  1. .leaf .v-tree8-cell-content {
  2. background-color:green
  3. }