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 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. IMPORTANT: The [classname]#TreeGrid# component is currently being developed and only available in the Framework 8.1 prerelease versions, starting from 8.1.0.alpha1.
  13. [[components.treegrid.overview]]
  14. == Overview
  15. [classname]#TreeGrid# is for displaying hierarchical tabular data laid out in rows and columns.
  16. It is otherwise identical to the [classname]#Grid# component, but it adds the possibility to show
  17. hierarchical data, allowing the user to expand and collapse nodes to show or hide data.
  18. See the documentation for <<dummy/../../../framework/components/components-grid.asciidoc#components.grid,"Grid">> for all the shared features between [classname]#Grid# and [classname]#TreeGrid#.
  19. [[figure.components.treegrid.basic]]
  20. .A [classname]#TreeGrid#
  21. image::img/tree-grid-basic.png[width=70%, scaledwidth=100%]
  22. [[components.treegrid.data]]
  23. == Binding to Data
  24. [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]#InMemoryHierarchicalDataProvider# 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
  25. <<dummy/../../../framework/datamodel/datamodel-hierarchical.asciidoc#datamodel.hierarchical,"Hierarchical Data">>.
  26. Populating a [classname]#TreeGrid# with in-memory data can be done as follows
  27. [source, java]
  28. ----
  29. Project rootProject = getRootRroject();
  30. HierarchyData<Project> data = new HierarchyData<>();
  31. // add a root level item with null parent
  32. data.addItem(null, rootProject);
  33. // Add all children for root item
  34. rootProject.flattened().forEach(
  35. project -> data.addItems(project, project.getSubProjects()));
  36. TreeGrid<Project> treeGrid = new TreeGrid<>();
  37. treeGrid.setDataProvider(new InMemoryHierarchicalDataProvider<>(data));
  38. // the first column gets the hierarchy indicator by default
  39. treeGrid.addColumn(Project::getName).setCaption("Project Name");
  40. treeGrid.addColumn(Project::getHoursDone).setCaption("Hours Done");
  41. treeGrid.addColumn(Project::getdLastModified).setCaption("Last Modified");
  42. ----
  43. The [classname]#HierarchyData# class can be used to build the hierarchical data structure,
  44. and it can then be passed on to [classname]#InMemoryHierarchicalDataProvider#. It is simply a hierarchical
  45. collection, that the data provider uses to populate the [classname]#TreeGrid#.
  46. The [methodname]#setItems# method in [classname]#TreeGrid# can be used to set the root level items. Internally
  47. an [classname]#InMemoryHierarchicalDataProvider# with [classname]#HierarchyData# is used. If at any time you want to modify the in-memory data in the grid, you may do it as follows
  48. [source, java]
  49. ----
  50. InMemoryHierarchicalDataProvider<Project> dataProvider = (InMemoryHierarchicalDataProvider<Project>) treeGrid.getDataProvider();
  51. HierarchyData<Project> data = dataProvider.getData();
  52. // add new items
  53. data.addItem(null, newProject);
  54. data.addItems(newProject, newProject.getChildren());
  55. // after adding / removing data, data provider needs to be refreshed
  56. dataProvider.refreshAll();
  57. ----
  58. 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.
  59. == Changing the Hierarchy Column
  60. By default, the [classname]#TreeGrid# shows the hierarchy indicator by default in the first column of the grid.
  61. 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#.
  62. [source, java]
  63. ----
  64. // the first column gets the hierarchy indicator by default
  65. treeGrid.addColumn(Project::getLastModified).setCaption("Last Modified");
  66. treeGrid.addColumn(Project::getName).setCaption("Project Name").setId("name");
  67. treeGrid.addColumn(Project::getHoursDone).setCaption("Hours Done");
  68. treeGrid.setHierarchyColumn("name");
  69. ----
  70. == Prevent Node Collapsing
  71. [classname]#TreeGrid# supports setting a callback method that can allow or prevent the user from collapsing an expanded node.
  72. It can be set with [methodname]#setItemCollapseAllowedProvider# method, that takes a [interfacename]#SerializablePredicate#.
  73. For nodes that cannot be collapsed, the [literal]#++collapse-disabled++# class name is applied to the expansion element
  74. Avoid doing any heavy operations in the method, since it is called for each item when it is being sent to the client.
  75. Example using a predefined set of persons that can not be collapsed:
  76. [source, java]
  77. ----
  78. Set<Person> alwaysExpanded;
  79. personTreeGrid.setItemCollapseAllowedProvider(person ->
  80. !alwaysExpanded.contains(person));
  81. ----
  82. == Listening to Events
  83. 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.
  84. The expand and collapse listeners can be added as follows:
  85. [source, java]
  86. ----
  87. treeGrid.addExpandListener(event -> log("Item expanded: " + event.getExpandedItem()));
  88. treeGrid.addCollapseListener(event -> log("Item collapsed: " + event.getCollapsedItem()));
  89. ----
  90. The return types of the methods `getExpandedItem` and `getCollapsedItem` are the same as the type of the [classname]#TreeGrid# the events originated from.
  91. Note that collapse listeners will not be triggered for any expanded subtrees of the collapsed item.