Docs

Documentation versions (currently viewingVaadin 25 (prerelease))

Add a Static Grid

Learn how to populate a grid with static data.

Static data refers to a fixed collection of objects held entirely in memory — such as reference data (countries, categories, status types) that is either hardcoded or loaded once at application startup. Java enum constants also fall into this category.

Because the full dataset is available in memory, the static data is loaded into the grid at once. Filtering and sorting can be performed without additional backend calls.

This guide starts with a complete example that you can copy-paste into your project if you want. It then breaks down the example into sections that explain how to populate, filter, and sort the grid. The guide does not cover setting up the Grid component itself; for that, see the Grid component documentation.

Copy-Paste into Your Project

If you want to quickly try out a static grid in your Vaadin application, copy-paste the following code into your project:

Source code
StaticGridView.java

For more detailed walk-through of the example code, continue reading below.

Getting the Data

In this example, the static data is a list of record objects returned by a simple service class:

Source code
StaticGridView.java

In a real world application, the service would be in its own file. The data could be statically defined as in this example, or loaded from a file or a database during application startup.

Populating the Grid

You populate the grid with static data by passing the collection of items to the grid’s setItems() method:

Source code
StaticGridView.java

The method returns a ListDataView object that you need if you want to filter the data. This is covered in the next section.

Filtering

To filter the grid, you need to add a filter to the ListDataView. A filter is a predicate that tests each item in the data set in memory. The filter is applied whenever the data view is refreshed, or when the filter itself is changed.

In this example, the filter checks whether the item’s name contains the filter string entered into the filter text field. If the filter string is blank, all items are shown:

Source code
StaticGridView.java
Note
The predicate executes once for each item in the data set. Because of this, you should keep the predicate as simple and efficient as possible. For instance, in the example above, the filter string is converted outside the predicate to avoid doing it repeatedly for each item.

For more information about filtering, see the Grid component documentation.

Sorting

Sorting happens in the grid automatically, for every column that is marked as sortable:

Source code
StaticGridView.java

For more information about sorting, see the Grid component documentation.