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.

UsingGridWithInlineData.asciidoc 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. ---
  2. title: Using Grid With Inline Data
  3. order: 33
  4. layout: page
  5. ---
  6. [[using-grid-with-inline-data]]
  7. = Using Grid with inline data
  8. Instead of using a Vaadin Container as explained in
  9. <<UsingGridWithAContainer#
  10. using-grid-with-a-container,Using Grid With a Container>>,
  11. you can also directly add simple inline data to Grid without directly
  12. using a Container.
  13. After creating a Grid instance, the first thing you need to do is to
  14. define the columns that should be shown. You an also define the types of
  15. the data in each column - Grid will expect String data in each column
  16. unless you do this.
  17. [source,java]
  18. ....
  19. grid.addColumn("Name").setSortable(true);
  20. grid.addColumn("Score", Integer.class);
  21. ....
  22. The columns will be shown in the order they are added. The `addColumn`
  23. method does also return the created `Column` instance, so you can go ahead
  24. and configure the column right away if you want to.
  25. When you have added all columns, you can add data using the
  26. `addRow(Object...)` method.
  27. [source,java]
  28. ....
  29. grid.addRow("Alice", 15);
  30. grid.addRow("Bob", -7);
  31. grid.addRow("Carol", 8);
  32. grid.addRow("Dan", 0);
  33. grid.addRow("Eve", 20);
  34. ....
  35. The order of the arguments to `addRow` should match the order in which the
  36. columns are shown. It is recommended to only use `addRow` when
  37. initializing Grid, since later on e.g. `setColumnOrder(Object...)` might
  38. have been used to change the order, causing unintended behavior.
  39. Grid will still manage a `Container` instance for you behind the scenes,
  40. so you can still use Grid API that is based on `Property` or `Item` from the
  41. `Container` API. One particularly useful feature is that each added row
  42. will get an `Integer` item id, counting up starting from 1. This means
  43. that you can e.g. select the second row in this way:
  44. [source,java]
  45. ....
  46. grid.select(2);
  47. ....
  48. [[full-example]]
  49. Full example
  50. ^^^^^^^^^^^^
  51. Putting all these pieces together, we end up with this class.
  52. [source,java]
  53. ....
  54. import com.vaadin.annotations.Theme;
  55. import com.vaadin.server.VaadinRequest;
  56. import com.vaadin.shared.ui.grid.HeightMode;
  57. import com.vaadin.ui.Grid;
  58. import com.vaadin.ui.UI;
  59. @Theme("valo")
  60. public class ShowingInlineDataInGrid extends UI {
  61. @Override
  62. protected void init(VaadinRequest request) {
  63. final Grid grid = new Grid();
  64. grid.addColumn("Name").setSortable(true);
  65. grid.addColumn("Score", Integer.class);
  66. grid.addRow("Alice", 15);
  67. grid.addRow("Bob", -7);
  68. grid.addRow("Carol", 8);
  69. grid.addRow("Dan", 0);
  70. grid.addRow("Eve", 20);
  71. grid.select(2);
  72. grid.setHeightByRows(grid.getContainerDataSource().size());
  73. grid.setHeightMode(HeightMode.ROW);
  74. setContent(grid);
  75. }
  76. }
  77. ....