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-treetable.asciidoc 3.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. ---
  2. title: TreeTable
  3. order: 23
  4. layout: page
  5. ---
  6. [[components.treetable]]
  7. = [classname]#TreeTable#
  8. ifdef::web[]
  9. [.sampler]
  10. image:{live-demo-image}[alt="Live Demo", link="http://demo.vaadin.com/sampler/#ui/grids-and-trees/tree-table"]
  11. endif::web[]
  12. [classname]#TreeTable# is an extension of the [classname]#Table# component with
  13. support for a tree-like hierarchy in the first column. As with
  14. [classname]#Tree#, the hierarchy is determined by the parent-children
  15. relationships defined in the [interfacename]#Container.Hierarchical# interface.
  16. The default container is [classname]#HierarchicalContainer#, but you can bind
  17. [classname]#TreeTable# to any container implementing the interface.
  18. [[figure.components.treetable.basic]]
  19. .The [classname]#TreeTable# component
  20. image::img/treetable-basic.png[width=40%, scaledwidth=60%]
  21. As with [classname]#Tree#, you can define the parent-child relationships with
  22. [methodname]#setParent()#, as is shown in the following example with numeric
  23. item IDs:
  24. [source, java]
  25. ----
  26. TreeTable ttable = new TreeTable("My TreeTable");
  27. ttable.addContainerProperty("Name", String.class, null);
  28. ttable.addContainerProperty("Number", Integer.class, null);
  29. // Create the tree nodes and set the hierarchy
  30. ttable.addItem(new Object[]{"Menu", null}, 0);
  31. ttable.addItem(new Object[]{"Beverages", null}, 1);
  32. ttable.setParent(1, 0);
  33. ttable.addItem(new Object[]{"Foods", null}, 2);
  34. ttable.setParent(2, 0);
  35. ttable.addItem(new Object[]{"Coffee", 23}, 3);
  36. ttable.addItem(new Object[]{"Tea", 42}, 4);
  37. ttable.setParent(3, 1);
  38. ttable.setParent(4, 1);
  39. ttable.addItem(new Object[]{"Bread", 13}, 5);
  40. ttable.addItem(new Object[]{"Cake", 11}, 6);
  41. ttable.setParent(5, 2);
  42. ttable.setParent(6, 2);
  43. ----
  44. Some container types may allow defining hierarchies if the container data
  45. itself, without explicitly setting the parent-child relationships with
  46. [methodname]#setParent()#.
  47. Unlike [classname]#Tree#, a [classname]#TreeTable# can have components in the
  48. hierarchical column, both when the property type is a component type and when
  49. the tree table is in editable mode.
  50. For other features, we refer you to documentation for [classname]#Table# in
  51. <<dummy/../../../framework/components/components-table#components.table,"Table">> and [classname]#Tree# in <<dummy/../../../framework/components/components-tree#components.tree,"Tree">>.
  52. ifdef::web[]
  53. [[components.treetable.collapsed]]
  54. == Expanding and Collapsing Items
  55. As in [classname]#Tree#, you can set the expanded/collapsed state of an item
  56. programmatically with [methodname]#setCollapsed()#. Note that if you want to
  57. expand all items, there is no [methodname]#expandItemsRecursively()# like in
  58. [classname]#Tree#. Moreover, the [methodname]#getItemIds()# only returns the IDs
  59. of the currently visible items for ordinary [interfacename]#Hierarchical# (not
  60. [interfacename]#Collapsible#) containers. Hence you can not use that to iterate
  61. over all the items, but you need to get the IDs from the underlying container.
  62. [source, java]
  63. ----
  64. // Expand the tree
  65. for (Object itemId: ttable.getContainerDataSource()
  66. .getItemIds()) {
  67. ttable.setCollapsed(itemId, false);
  68. // As we're at it, also disallow children from
  69. // the current leaves
  70. if (! ttable.hasChildren(itemId))
  71. ttable.setChildrenAllowed(itemId, false);
  72. }
  73. ----
  74. In large tables, this explicit setting becomes infeasible, as it needs to be
  75. stored in the [classname]#TreeTable# (more exactly, in the
  76. [classname]#HierarchicalStrategy# object) for all the contained items. You can
  77. use a [interfacename]#Collapsible# container to store the collapsed states in
  78. the container, thereby avoiding the explicit settings and memory overhead. There
  79. are no built-in collapsible containers in the Vaadin core framework, so you
  80. either need to use an add-on container or implement it yourself.
  81. endif::web[]