You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

components-tree.asciidoc 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. ---
  2. title: Tree
  3. order: 22
  4. layout: page
  5. ---
  6. [[components.tree]]
  7. = [classname]#Tree#
  8. ifdef::web[]
  9. [.sampler]
  10. image:{live-demo-image}[alt="Live Demo", link="http://demo.vaadin.com/sampler/#ui/grids-and-trees/tree"]
  11. endif::web[]
  12. The [classname]#Tree# component allows a natural way to represent data that has hierarchical relationships.
  13. The user can drill down in the hierarchy by expanding items by clicking on the expand arrow, and likewise collapse items.
  14. [classname]#Tree# is a selection component that allows selecting items.
  15. It also supports drag and drop, so you can drag items to and from a tree, and drop them in the hierarchy.
  16. A typical use of the [classname]#Tree# component is for displaying a hierarchical menu, as illustrated in <<figure.components.tree>>, or for displaying file systems or hierarchical datasets.
  17. [[figure.components.tree]]
  18. .A [classname]#Tree# component as a menu
  19. image::img/tree-example1.png[width=25%, scaledwidth=50%]
  20. The data is managed in a container implementing the [interfacename]#Hierarchical# interface, such as [classname]#HierarchicalContainer# or [classname]#FilesystemContainer#.
  21. You can use [classname]#ContainerHierarchicalWrapper# to add hierarchical capability to any other container. [classname]#Tree# itself implements the interface and delegates operations to the underlying container.
  22. [source, java]
  23. ----
  24. // A menu tree
  25. Tree menu = new Tree();
  26. // Couple of childless root items
  27. menu.addItem("Mercury");
  28. menu.setChildrenAllowed("Mercury", false);
  29. menu.addItem("Venus");
  30. menu.setChildrenAllowed("Venus", false);
  31. // An item with hierarchy
  32. menu.addItem("Earth");
  33. menu.addItem("The Moon");
  34. menu.setChildrenAllowed("The Moon", false);
  35. menu.setParent("The Moon", "Earth");
  36. menu.expandItem("Earth"); // Expand programmatically
  37. ...
  38. ----
  39. The result was shown in <<figure.components.tree>> in a practical situation, with the [classname]`Tree` wrapped inside a [classname]`Panel`.
  40. [classname]`Tree` itself does not have scrollbar, but [classname]`Panel` can be used for the purpose.
  41. The caption of tree items is by default the item ID.
  42. You can define how the item captions are determined with [methodname]#setItemCaptionMode()#, as explained <<components-selection#components.selection.captions, "Selection Component Item Captions">>.
  43. [[components.tree.selection]]
  44. == Handling Selection and Clicks
  45. [classname]#Tree# is a selection component, which are described in <<components-selection#components.selection, "Selection Components">>.
  46. You can thereby get or set the currently selected item by the value property of the tree, that is, with [methodname]#getValue()# and [methodname]#setValue()#.
  47. When the user selects an item, the tree will receive an [classname]#ValueChangeEvent#, which you can catch with a [classname]#ValueChangeListener#.
  48. [source, Java]
  49. ----
  50. // Handle selection changes
  51. menu.addValueChangeListener(event -> { // Java 8
  52. if (event.getProperty() != null &&
  53. event.getProperty().getValue() != null) {
  54. location.setValue("The cat is in " +
  55. event.getProperty().getValue());
  56. }
  57. });
  58. ----
  59. [classname]#Tree# is selectable by default; you can disallow selection with [methodname]#setSelectable(false)#.
  60. [classname]#Tree# also emits [classname]##ItemClickEvent##s when items are clicked.
  61. This way you can handle item clicks also when selection is not enabled or you want special user interaction specifically on clicks.
  62. [source, Java]
  63. ----
  64. tree.addItemClickListener(
  65. new ItemClickEvent.ItemClickListener() {
  66. public void itemClick(ItemClickEvent event) {
  67. // Pick only left mouse clicks
  68. if (event.getButton() == ItemClickEvent.BUTTON_LEFT)
  69. Notification.show("Left click",
  70. Notification.Type.HUMANIZED_MESSAGE);
  71. }
  72. });
  73. ----
  74. [[components.tree.expand-collapse]]
  75. == Expanding and Collapsing Items
  76. An item can have children only if the [propertyname]#childrenAllowed# property is set as true.
  77. The expand indicator is shown when and only when the property is true.
  78. The property is defined in the container and can be set with [methodname]#setChildrenAllowed()#.
  79. Expanding an item fires an [classname]#Tree.ExpandEvent# and collapsing an [classname]#Tree.CollapseEvent#, which you can handle with respective listeners.
  80. [source, Java]
  81. ----
  82. tree.addExpandListener(new Tree.ExpandListener() {
  83. public void nodeExpand(ExpandEvent event) {
  84. Notification.show("Expand!");
  85. }
  86. });
  87. ----
  88. You can expand and collapse items programmatically with [methodname]#expandItem()# or [methodname]#expandItemRecursively()#.
  89. [source, Java]
  90. ----
  91. // Expand all items that can be
  92. for (Object itemId: tree.getItemIds())
  93. tree.expandItem(itemId);
  94. ----
  95. TIP: [classname]#Tree# itself does not support lazy loading, which makes it impractical for huge hierarchies.
  96. You can implement one kind of lazy loading by adding items in an expand listener and removing them in a collapse listener.
  97. For more proper lazy loading, you can use [classname]#TreeTable# or hierarchical support extension for [classname]#Grid#.
  98. [[components.tree.css]]
  99. == CSS Style Rules
  100. [source, css]
  101. ----
  102. .v-tree {}
  103. .v-tree-node {} /* A node (item) */
  104. .v-tree-node-caption {} /* Caption of the node */
  105. .v-tree-node-children {} /* Contains child nodes */
  106. .v-tree-node-root {} /* If node is a root node */
  107. .v-tree-node-leaf {} /* If node has no children */
  108. ----
  109. [[components.tree.css.itemstyles]]
  110. === Generating Item Styles
  111. You can style each tree item individually by generating a style name for them with a [interfacename]#Tree.ItemStyleGenerator#, which you assign to a tree with [methodname]#setItemStyleGenerator()#.
  112. The generator should return a style name for each item or `null`.
  113. [source, Java]
  114. ----
  115. // Show all leaf nodes as disabled
  116. tree.setItemStyleGenerator(new Tree.ItemStyleGenerator() {
  117. @Override
  118. public String getStyle(Tree source, Object itemId) {
  119. if (! tree.hasChildren(itemId))
  120. return "disabled";
  121. return null;
  122. }
  123. });
  124. ----
  125. The style names are prefixed with `v-tree-node-caption-`.
  126. You could thereby define the item styling as follows:
  127. [source, CSS]
  128. ----
  129. .v-tree-node-caption-disabled {
  130. color: graytext;
  131. font-style: italic;
  132. }
  133. ----