Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

components-treetable.asciidoc 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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()#. These containers should be used if there is going
  47. to be a lot of data. Setting up a [interfacename]#Container.Hierarchical# before
  48. setting it to the [classname]#TreeTable# will improve the initial performance.
  49. Unlike [classname]#Tree#, a [classname]#TreeTable# can have components in the
  50. hierarchical column, both when the property type is a component type and when
  51. the tree table is in editable mode.
  52. For other features, we refer you to documentation for [classname]#Table# in
  53. <<dummy/../../../framework/components/components-table#components.table,"Table">> and [classname]#Tree# in <<dummy/../../../framework/components/components-tree#components.tree,"Tree">>.
  54. ifdef::web[]
  55. [[components.treetable.collapsed]]
  56. == Expanding and Collapsing Items
  57. As in [classname]#Tree#, you can set the expanded/collapsed state of an item
  58. programmatically with [methodname]#setCollapsed()#. Note that if you want to
  59. expand all items, there is no [methodname]#expandItemsRecursively()# like in
  60. [classname]#Tree#. Moreover, the [methodname]#getItemIds()# only returns the IDs
  61. of the currently visible items for ordinary [interfacename]#Hierarchical# (not
  62. [interfacename]#Collapsible#) containers. Hence you can not use that to iterate
  63. over all the items, but you need to get the IDs from the underlying container.
  64. [source, java]
  65. ----
  66. // Expand the tree
  67. for (Object itemId: ttable.getContainerDataSource()
  68. .getItemIds()) {
  69. ttable.setCollapsed(itemId, false);
  70. // As we're at it, also disallow children from
  71. // the current leaves
  72. if (! ttable.hasChildren(itemId))
  73. ttable.setChildrenAllowed(itemId, false);
  74. }
  75. ----
  76. In large tables, this explicit setting becomes infeasible, as it needs to be
  77. stored in the [classname]#TreeTable# (more exactly, in the
  78. [classname]#HierarchicalStrategy# object) for all the contained items. You can
  79. use a [interfacename]#Collapsible# container to store the collapsed states in
  80. the container, thereby avoiding the explicit settings and memory overhead. There
  81. are no built-in collapsible containers in the Vaadin core framework, so you
  82. either need to use an add-on container or implement it yourself.
  83. endif::web[]