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-treegrid.asciidoc 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. ---
  2. title: TreeGrid
  3. order: 25
  4. layout: page
  5. ---
  6. [[components.treegrid]]
  7. = TreeGrid
  8. ifdef::web[]
  9. [.sampler]
  10. image:{live-demo-image}[alt="Live Demo", link="http://demo.vaadin.com/sampler/#ui/grids-and-trees/treegrid"]
  11. endif::web[]
  12. [[components.treegrid.overview]]
  13. == Overview
  14. [classname]#TreeGrid# is for displaying hierarchical tabular data laid out in rows and columns.
  15. It is otherwise identical to the [classname]#Grid# component, but it adds the possibility to show
  16. hierarchical data, allowing the user to expand and collapse nodes to show or hide data.
  17. See the documentation for <<dummy/../../../framework/components/components-grid.asciidoc#components.grid,"Grid">> for all the shared features between [classname]#Grid# and [classname]#TreeGrid#.
  18. [[figure.components.treegrid.basic]]
  19. .A [classname]#TreeGrid#
  20. image::img/tree-grid-basic.png[width=70%, scaledwidth=100%]
  21. [[components.treegrid.data]]
  22. == Binding to Data
  23. [classname]#TreeGrid# 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. <<dummy/../../../framework/datamodel/datamodel-hierarchical.asciidoc#datamodel.hierarchical,"Hierarchical Data">>.
  25. Populating a [classname]#TreeGrid# with in-memory data can be done as follows
  26. [source, java]
  27. ----
  28. // Initialize a TreeGrid and set in-memory data
  29. TreeGrid<Project> treeGrid = new TreeGrid<>();
  30. treeGrid.setItems(getRootProjects(), Project::getSubProjects);
  31. // The first column gets the hierarchy indicator by default
  32. treeGrid.addColumn(Project::getName).setCaption("Project Name");
  33. treeGrid.addColumn(Project::getHoursDone).setCaption("Hours Done");
  34. treeGrid.addColumn(Project::getLastModified).setCaption("Last Modified");
  35. ----
  36. The [classname]#TreeData# class can be used to build the hierarchical data structure,
  37. and it can then be passed on to [classname]#TreeDataProvider#. It is simply a hierarchical
  38. collection, that the data provider uses to populate the [classname]#TreeGrid#.
  39. The [methodname]#setItems# method in [classname]#TreeGrid# can be used to set the root level items. Internally
  40. an [classname]#TreeDataProvider# with [classname]#TreeData# is used. If at any time you want to modify the in-memory data in the grid, you may do it as follows
  41. [source, java]
  42. ----
  43. TreeDataProvider<Project> dataProvider = (TreeDataProvider<Project>) treeGrid.getDataProvider();
  44. TreeData<Project> data = dataProvider.getTreeData();
  45. // add new items
  46. data.addItem(null, newProject);
  47. data.addItems(newProject, newProject.getChildren());
  48. // after adding / removing data, data provider needs to be refreshed
  49. dataProvider.refreshAll();
  50. ----
  51. Note that for adding or removing nodes, you always need to call the [methodname]#refreshAll# method in the data provider you are using. The [methodname]#refreshItem# method can only be used when just the data for that item is updated, but not for updates that add or remove items.
  52. [[components.treegrid.expandcollapse]]
  53. == Expanding and Collapsing Nodes
  54. [classname]#TreeGrid# nodes that have children can be expanded and collapsed by either user interaction or through the server-side API:
  55. [source, java]
  56. ----
  57. // Expands a child project. If the child project is not yet
  58. // in the visible hierarchy, nothing will be shown.
  59. treeGrid.expand(childProject);
  60. // Expands the root project. If child project now becomes
  61. // visible it is also expanded into view.
  62. treeGrid.expand(rootProject);
  63. // Collapses the child project.
  64. treeGrid.collapse(childProject);
  65. ----
  66. 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#.
  67. [[components.treegrid.hierarchycolumn]]
  68. == Changing the Hierarchy Column
  69. By default, the [classname]#TreeGrid# shows the hierarchy indicator by default in the first column of the grid.
  70. You can change it with with the [methodname]#setHierarchyColumn#, method, that takes as a parameter the column's ID specified with the [methodname]#setId# method in [classname]#Column#.
  71. [source, java]
  72. ----
  73. // the first column gets the hierarchy indicator by default
  74. treeGrid.addColumn(Project::getLastModified).setCaption("Last Modified");
  75. treeGrid.addColumn(Project::getName).setCaption("Project Name").setId("name");
  76. treeGrid.addColumn(Project::getHoursDone).setCaption("Hours Done");
  77. treeGrid.setHierarchyColumn("name");
  78. ----
  79. [[components.treegrid.node.collapsing]]
  80. == Prevent Node Collapsing
  81. [classname]#TreeGrid# supports setting a callback method that can allow or prevent the user from collapsing an expanded node.
  82. It can be set with [methodname]#setItemCollapseAllowedProvider# method, that takes a [interfacename]#SerializablePredicate#.
  83. For nodes that cannot be collapsed, the [literal]#++collapse-disabled++# class name is applied to the expansion element
  84. Avoid doing any heavy operations in the method, since it is called for each item when it is being sent to the client.
  85. Example using a predefined set of persons that can not be collapsed:
  86. [source, java]
  87. ----
  88. Set<Person> alwaysExpanded;
  89. personTreeGrid.setItemCollapseAllowedProvider(person ->
  90. !alwaysExpanded.contains(person));
  91. ----
  92. [[components.treegrid.events]]
  93. == Listening to Events
  94. In addition to supporting all the listeners of the standard [classname]#Grid#, [classname]#TreeGrid# supports listening to the expansion and collapsing of items in its hierarchy.
  95. The expand and collapse listeners can be added as follows:
  96. [source, java]
  97. ----
  98. treeGrid.addExpandListener(event -> log("Item expanded: " + event.getExpandedItem()));
  99. treeGrid.addCollapseListener(event -> log("Item collapsed: " + event.getCollapsedItem()));
  100. ----
  101. The return types of the methods `getExpandedItem` and `getCollapsedItem` are the same as the type of the [classname]#TreeGrid# the events originated from.
  102. Note that collapse listeners will not be triggered for any expanded subtrees of the collapsed item.
  103. [[components.treegrid.keyboard]]
  104. == Keyboard Navigation and Focus Handling in TreeGrid
  105. As opposed to [classname]#Grid#, individual cells are not focusable in [classname]#TreeGrid#, and only whole rows
  106. receive focus. The user can navigate through rows with kbd:[Up] and kbd:[Down], collapse rows with kbd:[Left],
  107. and expand them with kbd:[Right].