Docs

Binding Data to Grid Flow

Binding and displaying data in Grid

Grid supports connecting to various types of data sources through data providers. A data provider is a class that implements the DataProvider interface and encapsulates the logic for fetching items from a specific data source, with support for pagination, sorting, and filtering. For common use cases, Grid provides convenience methods so that you don’t have to deal with the full data provider setup, and it also supports custom data providers where more flexibility is needed.

Binding Items via setItems

The most straightforward way to bind data to a Grid is to use the setItems convenience method. It allows you to bind an in-memory collection or provide callbacks for lazy loading from a backend service without the need to explicitly create a data provider.

The example below shows how to display, filter and sort a static list of Person records:

Source code
PeopleView.java
Person.java

The next example shows how to display, filter and sort data from a backend service with lazy loading by providing fetch and count callbacks to the setItems method:

Source code
PeopleView.java
PersonService.java
Person.java

More documentation and examples for using the setItems method are available in the Binding Items To Components article.

Custom Data Providers

A more advanced way to bind data to a Grid is to supply a custom data provider implementation via the setDataProvider(DataProvider<T, F>) method.

The example below shows a Grid backed by a custom data provider that imitates fetching data from a database, with support for filtering and sorting:

Source code
PersonDataProvider.java
PersonFilter.java
Person.java
PeopleView.java

More examples of custom data providers are available in the Binding Items To Components article.

Binding Maps Instead of Beans

Grid items don’t need to be beans or records. When the columns are only known at runtime — for example, when they come from a configuration or a query result — you can use maps as items, and define the columns and the editor bindings with lambda expressions instead of property names or method references.

The example below shows a Grid with map items, where the month columns are generated from a list, and the cells can be edited with the inline editor:

Source code
BindingMapItems.java

The equality and hash code of a map depend on its content, which changes when an item is edited. Therefore, the example calls setIdentifierProvider() on the data view returned by setItems() to identify each item by a stable key — here, the product name. The Grid uses this identifier instead of equals() and hashCode() to track items, so editing and refreshing an item keeps working after its content changes.

Updated –