選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

components-tree.asciidoc 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. ---
  2. title: Tree
  3. order: 26
  4. layout: page
  5. ---
  6. [[components.tree]]
  7. = 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. [[components.tree.overview]]
  13. == Overview
  14. The [classname]#Tree# component allows a natural way to represent data that has hierarchical relationships.
  15. The user can drill down in the hierarchy by expanding items by clicking on the expand arrow, and likewise collapse items.
  16. [classname]#Tree# is a selection component that allows selecting items.
  17. 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.
  18. [[figure.components.tree]]
  19. .A [classname]#Tree# component
  20. image::img/tree-basic.png[width=70%, scaledwidth=100%]
  21. [[components.tree.data]]
  22. == Binding to Data
  23. [classname]#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 [classname]#TreeDataProvider# can be used, and for loading data from a back end, you need to implement three methods from the [interfacename]#HierarchicalDataProvider# interface. Usage of both data providers is described in
  24. <<../datamodel/datamodel-hierarchical.asciidoc#datamodel.hierarchical,"Hierarchical Data">>.
  25. The [classname]#TreeData# class can be used to build the hierarchical data structure,
  26. and it can then be passed on to [classname]#TreeDataProvider#. It is simply a hierarchical
  27. collection, that the data provider uses to populate the [classname]#Tree#.
  28. The [methodname]#setItems# method in [classname]#Tree# can be used to set the root level items. Internally
  29. an [classname]#TreeDataProvider# with [classname]#TreeData# is used.
  30. [source, java]
  31. ----
  32. // An initial planet tree
  33. Tree<String> tree = new Tree<>();
  34. TreeData<String> treeData = new TreeData<>();
  35. // Couple of childless root items
  36. treeData.addItem(null,"Mercury");
  37. treeData.addItem(null,"Venus");
  38. // Items with hierarchy
  39. treeData.addItem(null,"Earth");
  40. treeData.addItem("Earth","The Moon");
  41. inMemoryDataProvider = new TreeDataProvider<>(treeData);
  42. tree.setDataProvider(inMemoryDataProvider);
  43. tree.expand("Earth"); // Expand programmatically
  44. ----
  45. If at any time you want to modify
  46. the in-memory data in the tree, you may do it as follows:
  47. [source, java]
  48. ----
  49. // Add Mars with satellites
  50. treeData.addItem(null, "Mars");
  51. treeData.addItem("Mars", "Phobos");
  52. treeData.addItem("Mars", "Deimos");
  53. inMemoryDataProvider.refreshAll();
  54. ----
  55. The result was shown in <<figure.components.tree>>.
  56. The caption and the icon of tree items is generated by the [classname]#ItemCaptionGenerator# and the
  57. [classname]#IconGenerator#, set with [methodname]#setItemCaptionGenerator()# and [methodname]#setItemIconGenerator()# respectively.
  58. [[components.tree.selection]]
  59. == Handling Selection and Clicks
  60. [classname]#Tree# supports single selection mode, you can use [methodname]#asSingleSelect()# to access the selection
  61. object, which supports selection listeners and data binding. For more details, see link:<<../datamodel/datamodel-selection.asciidoc#datamodel.selection,"Selecting Items">>.
  62. The [classname]#Tree# also supports the shortcut method [methodname]#addSelectionListener()#.
  63. [classname]#Tree# also emits [classname]##ItemClickEvent##s when items are clicked.
  64. This way you can handle item clicks also when you want special user interaction specifically on clicks.
  65. [source, Java]
  66. ----
  67. tree.addItemClickListener(event ->
  68. Notification.show("Click",
  69. Notification.Type.HUMANIZED_MESSAGE)
  70. );
  71. ----
  72. [[components.tree.right.clicks]]
  73. === Right-clicks
  74. Right-clicks are supported similar way via `addContextClickListener()` method
  75. [source, java]
  76. ----
  77. tree.addContextClickListener(event -> Notification.show(
  78. ((TreeContextClickEvent<Person>)event).getItem() + " Clicked")
  79. );
  80. ----
  81. [[components.tree.expandcollapse]]
  82. == Expanding and Collapsing Nodes
  83. [classname]#Tree# nodes that have children can be expanded and collapsed by either user interaction or through the server-side API:
  84. [source, java]
  85. ----
  86. // Expands a child project. If the child project is not yet
  87. // in the visible hierarchy, nothing will be shown.
  88. tree.expand(childProject);
  89. // Expands the root project. If child project now becomes
  90. // visible it is also expanded into view.
  91. tree.expand(rootProject);
  92. // Collapses the child project.
  93. tree.collapse(childProject);
  94. ----
  95. To use the server-side API with a backend data provider the [methodname]#hashCode# and [methodname]#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 [methodname]#expand# or [methodname]#collapse#.
  96. The [classname]#Tree# component supports listening to the expansion and collapsing of items in its hierarchy.
  97. The expand and collapse listeners can be added as follows:
  98. [source, java]
  99. ----
  100. tree.addExpandListener(event -> log("Item expanded: " + event.getExpandedItem()));
  101. tree.addCollapseListener(event -> log("Item collapsed: " + event.getCollapsedItem()));
  102. ----
  103. The return types of the methods `getExpandedItem` and `getCollapsedItem` are the same as the type of the [classname]#Tree# the events originated from.
  104. Note that collapse listeners will not be triggered for any expanded subtrees of the collapsed item.
  105. [[components.tree.node.collapsing]]
  106. == Prevent Node Collapsing
  107. [classname]#Tree# supports setting a callback method that can allow or prevent the user from collapsing an expanded node.
  108. It can be set with [methodname]#setItemCollapseAllowedProvider# method, that takes a [interfacename]#ItemCollapseAllowedProvider#.
  109. For nodes that cannot be collapsed, the [literal]#++collapse-disabled++# class name is applied to the expansion element
  110. Avoid doing any heavy operations in the method, since it is called for each item when it is being sent to the client.
  111. Example using a predefined set of persons that can not be collapsed:
  112. [source, java]
  113. ----
  114. Set<Person> alwaysExpanded;
  115. personTree.setItemCollapseAllowedProvider(person ->
  116. !alwaysExpanded.contains(person));
  117. ----
  118. [[components.treegrid.keyboard]]
  119. == Keyboard Navigation and Focus Handling in TreeGrid
  120. The user can navigate through rows with kbd:[Up] and kbd:[Down], collapse rows with kbd:[Left],
  121. and expand them with kbd:[Right].
  122. [[components.tree.css]]
  123. == CSS Style Rules
  124. [source, css]
  125. ----
  126. .v-tree8 {
  127. .v-tree8-scroller, .v-tree8-scroller-horizontal { }
  128. .v-tree8-tablewrapper {
  129. .v-tree8-body {
  130. .v-tree8-row,
  131. .v-tree8-stripe,
  132. .v-tree8-row-focused,
  133. .v-tree8-row-has-data {
  134. .v-tree8-expander, expanded {}
  135. .v-tree8-cell-content {}
  136. }
  137. }
  138. }
  139. }
  140. ----
  141. [[components.tree.css.itemstyles]]
  142. === Generating Item Styles
  143. You can style each tree item individually by generating a style name for them with a [interfacename]#StyleGenerator#, which you assign to a tree with [methodname]#setStyleGenerator()#.
  144. The generator should return a style name for each item or `null`.
  145. [source, Java]
  146. ----
  147. // Show all leaf nodes as disabled
  148. tree.setStyleGenerator(item -> {
  149. if (!tree.getDataProvider().hasChildren(item))
  150. return "leaf";
  151. return null;
  152. }
  153. );
  154. ----
  155. [source, css]
  156. ----
  157. .leaf .v-tree8-cell-content {
  158. background-color:green
  159. }
  160. ----