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.

UsingGridWithAContainer.asciidoc 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. ---
  2. title: Using Grid With A Container
  3. order: 31
  4. layout: page
  5. ---
  6. [[using-grid-with-a-container]]
  7. = Using Grid with a Container
  8. Grid lazy-loads data from a `Container` instance. There are different
  9. container implementations that e.g. fetch data from a database or use a
  10. list of Java objects. Assuming you already have code that initializes a
  11. `Container`, this is all that is needed for showing a Grid with the data
  12. from your container.
  13. [source,java]
  14. ....
  15. import com.vaadin.server.VaadinRequest;
  16. import com.vaadin.ui.Grid;
  17. import com.vaadin.ui.UI;
  18. public class UsingGridWithAContainer extends UI {
  19. @Override
  20. protected void init(VaadinRequest request) {
  21. Grid grid = new Grid();
  22. grid.setContainerDataSource(GridExampleHelper.createContainer());
  23. setContent(grid);
  24. }
  25. }
  26. ....
  27. The container in this example contains three properties; name, count and
  28. amount. You can configure the columns in Grid using the property ids to
  29. do things like setting the column caption, removing a column or changing
  30. the order of the visible columns.
  31. [source,java]
  32. ....
  33. protected void init(VaadinRequest request) {
  34. Grid grid = new Grid();
  35. grid.setContainerDataSource(GridExampleHelper.createContainer());
  36. grid.getColumn("name").setHeaderCaption("Bean name");
  37. grid.removeColumn("count");
  38. grid.setColumnOrder("name", "amount");
  39. setContent(grid);
  40. }
  41. ....
  42. This is really all that is needed to get started with Grid.
  43. For reference, this is how the example container is implemented.
  44. [source,java]
  45. ....
  46. public class GridExampleBean {
  47. private String name;
  48. private int count;
  49. private double amount;
  50. public GridExampleBean() {
  51. }
  52. public GridExampleBean(String name, int count, double amount) {
  53. this.name = name;
  54. this.count = count;
  55. this.amount = amount;
  56. }
  57. public String getName() {
  58. return name;
  59. }
  60. public int getCount() {
  61. return count;
  62. }
  63. public double getAmount() {
  64. return amount;
  65. }
  66. public void setName(String name) {
  67. this.name = name;
  68. }
  69. public void setCount(int count) {
  70. this.count = count;
  71. }
  72. public void setAmount(double amount) {
  73. this.amount = amount;
  74. }
  75. }
  76. ....
  77. [source,java]
  78. ....
  79. import com.vaadin.data.util.BeanItemContainer;
  80. public class GridExampleHelper {
  81. public static BeanItemContainer<GridExampleBean> createContainer() {
  82. BeanItemContainer<GridExampleBean> container = new BeanItemContainer<GridExampleBean>(
  83. GridExampleBean.class);
  84. for (int i = 0; i < 1000; i++) {
  85. container.addItem(new GridExampleBean("Bean " + i, i * i, i / 10d));
  86. }
  87. return container;
  88. }
  89. }
  90. ....