summaryrefslogtreecommitdiffstats
path: root/documentation/articles/UsingGridWithAContainer.asciidoc
blob: a3eb58f4610cd626c943f80a00bcde3f1a682b14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
---
title: Using Grid With A Container
order: 31
layout: page
---

[[using-grid-with-a-container]]
Using Grid with a Container
---------------------------

Grid lazy-loads data from a `Container` instance. There are different
container implementations that e.g. fetch data from a database or use a
list of Java objects. Assuming you already have code that initializes a
`Container`, this is all that is needed for showing a Grid with the data
from your container.

[source,java]
....
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.Grid;
import com.vaadin.ui.UI;

public class UsingGridWithAContainer extends UI {
  @Override
  protected void init(VaadinRequest request) {
    Grid grid = new Grid();
    grid.setContainerDataSource(GridExampleHelper.createContainer());

    setContent(grid);
  }
}
....

The container in this example contains three properties; name, count and
amount. You can configure the columns in Grid using the property ids to
do things like setting the column caption, removing a column or changing
the order of the visible columns.

[source,java]
....
protected void init(VaadinRequest request) {
  Grid grid = new Grid();
  grid.setContainerDataSource(GridExampleHelper.createContainer());

  grid.getColumn("name").setHeaderCaption("Bean name");
  grid.removeColumn("count");
  grid.setColumnOrder("name", "amount");

  setContent(grid);
}
....

This is really all that is needed to get started with Grid.

For reference, this is how the example container is implemented.

[source,java]
....
public class GridExampleBean {
  private String name;
  private int count;
  private double amount;

  public GridExampleBean() {
  }

  public GridExampleBean(String name, int count, double amount) {
    this.name = name;
    this.count = count;
    this.amount = amount;
  }

  public String getName() {
    return name;
  }

  public int getCount() {
    return count;
  }

  public double getAmount() {
    return amount;
  }

  public void setName(String name) {
    this.name = name;
  }

  public void setCount(int count) {
    this.count = count;
  }

  public void setAmount(double amount) {
    this.amount = amount;
  }
}
....

[source,java]
....
import com.vaadin.data.util.BeanItemContainer;

public class GridExampleHelper {
  public static BeanItemContainer<GridExampleBean> createContainer() {
    BeanItemContainer<GridExampleBean> container = new BeanItemContainer<GridExampleBean>(
        GridExampleBean.class);
    for (int i = 0; i < 1000; i++) {
      container.addItem(new GridExampleBean("Bean " + i, i * i, i / 10d));
    }
    return container;
  }
}
....