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.

ShowingExtraDataForGridRows.asciidoc 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. ---
  2. title: Showing Extra Data For Grid Rows
  3. order: 29
  4. layout: page
  5. ---
  6. [[showing-extra-data-for-grid-rows]]
  7. = Showing extra data for Grid rows
  8. Some data might not be suitable to be shown as part of a regular Grid,
  9. e.g. because it's too large to fit into a Grid cell or because it's
  10. secondary information that should only be shown on demand. This kind of
  11. situation is covered with the row details functionality that shows a
  12. Vaadin Component in an area expanded below a specific row. Using this
  13. functionality is a two step process: first you need to implement a
  14. generator that lazily creates the `Component` for a row if it has been
  15. expanded, and then you need to hook up the events for actually expanding
  16. a row.
  17. This example uses the same data as in the
  18. <<UsingGridWithAContainer#using-grid-with-a-container,
  19. Using Grid with a Container>> example.
  20. [[detailsgenerator]]
  21. DetailsGenerator
  22. ^^^^^^^^^^^^^^^^
  23. A details generator is a callback interface that Grid calls to create
  24. the Vaadin `Component` that is used for showing the details for a specific
  25. row. In this example, we create a layout that contains a label, an image
  26. and a button that all use data from the row.
  27. [source,java]
  28. ....
  29. grid.setDetailsGenerator(new DetailsGenerator() {
  30. @Override
  31. public Component getDetails(RowReference rowReference) {
  32. // Find the bean to generate details for
  33. final GridExampleBean bean = (GridExampleBean) rowReference.getItemId();
  34. // A basic label with bean data
  35. Label label = new Label("Extra data for " + bean.getName());
  36. // An image with extra details about the bean
  37. Image image = new Image();
  38. image.setWidth("300px");
  39. image.setHeight("150px");
  40. image.setSource(new ExternalResource("http://dummyimage.com/300x150/000/fff&text=" + bean.getCount()));
  41. // A button just for the sake of the example
  42. Button button = new Button("Click me", new Button.ClickListener() {
  43. @Override
  44. public void buttonClick(ClickEvent event) {
  45. Notification.show("Button clicked for " + bean.getName());
  46. }
  47. });
  48. // Wrap up all the parts into a vertical layout
  49. VerticalLayout layout = new VerticalLayout(label, image, button);
  50. layout.setSpacing(true);
  51. layout.setMargin(true);
  52. return layout;
  53. }
  54. });
  55. ....
  56. [[opening-the-details-for-a-row]]
  57. Opening the details for a row
  58. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  59. Since there are multiple different UI patterns for how details should be
  60. opened (e.g. clicking a button in a cell or double clicking anywhere on
  61. the row), Grid does not have any action enabled by default. You can
  62. instead implement your own listener that takes care of showing and
  63. hiding the details for the rows. One easy way of doing this is to add an
  64. item click listener that toggles the status whenever a row is double
  65. clicked.
  66. [source,java]
  67. ....
  68. grid.addItemClickListener(new ItemClickListener() {
  69. @Override
  70. public void itemClick(ItemClickEvent event) {
  71. if (event.isDoubleClick()) {
  72. Object itemId = event.getItemId();
  73. grid.setDetailsVisible(itemId, !grid.isDetailsVisible(itemId));
  74. }
  75. }
  76. });
  77. ....
  78. [[full-example]]
  79. Full example
  80. ^^^^^^^^^^^^
  81. Putting all these pieces together, we end up with this class that uses
  82. the same data as in the link:UsingGridWithAContainer.asciidoc[Using
  83. Grid with a Container] example.
  84. [source,java]
  85. ....
  86. import com.vaadin.event.ItemClickEvent;
  87. import com.vaadin.event.ItemClickEvent.ItemClickListener;
  88. import com.vaadin.server.ExternalResource;
  89. import com.vaadin.server.VaadinRequest;
  90. import com.vaadin.ui.Button;
  91. import com.vaadin.ui.Button.ClickEvent;
  92. import com.vaadin.ui.Component;
  93. import com.vaadin.ui.Grid;
  94. import com.vaadin.ui.Grid.DetailsGenerator;
  95. import com.vaadin.ui.Grid.RowReference;
  96. import com.vaadin.ui.Image;
  97. import com.vaadin.ui.Label;
  98. import com.vaadin.ui.Notification;
  99. import com.vaadin.ui.UI;
  100. import com.vaadin.ui.VerticalLayout;
  101. public class ShowingExtraDataForRows extends UI {
  102. @Override
  103. protected void init(VaadinRequest request) {
  104. final Grid grid = new Grid();
  105. grid.setContainerDataSource(GridExampleHelper.createContainer());
  106. grid.setDetailsGenerator(new DetailsGenerator() {
  107. @Override
  108. public Component getDetails(RowReference rowReference) {
  109. // Find the bean to generate details for
  110. final GridExampleBean bean = (GridExampleBean) rowReference.getItemId();
  111. // A basic label with bean data
  112. Label label = new Label("Extra data for " + bean.getName());
  113. // An image with extra details about the bean
  114. Image image = new Image();
  115. image.setWidth("300px");
  116. image.setHeight("150px");
  117. image.setSource(new ExternalResource("http://dummyimage.com/300x150/000/fff&text=" + bean.getCount()));
  118. // A button just for the sake of the example
  119. Button button = new Button("Click me", new Button.ClickListener() {
  120. @Override
  121. public void buttonClick(ClickEvent event) {
  122. Notification.show("Button clicked for " + bean.getName());
  123. }
  124. });
  125. // Wrap up all the parts into a vertical layout
  126. VerticalLayout layout = new VerticalLayout(label, image, button);
  127. layout.setSpacing(true);
  128. layout.setMargin(true);
  129. return layout;
  130. }
  131. });
  132. grid.addItemClickListener(new ItemClickListener() {
  133. @Override
  134. public void itemClick(ItemClickEvent event) {
  135. if (event.isDoubleClick()) {
  136. Object itemId = event.getItemId();
  137. grid.setDetailsVisible(itemId, !grid.isDetailsVisible(itemId));
  138. }
  139. }
  140. });
  141. setContent(grid);
  142. }
  143. }
  144. ....