Add a Buffered Grid
Buffered data refers to data that is loaded from an application service where the entire filtered result set is fetched at once. This approach works well when you can guarantee that filtered result sets remain small enough to fit in memory (for example, orders for a single customer, or tasks assigned to a user).
As with static data, buffered data is loaded into the grid at once and sorted in memory. However, the filtering happens in the application service.
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 guides 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 buffered grid in your Vaadin application, copy-paste the following code into your project:
Source code
BufferedGridView.java
For more detailed walk-through of the example code, continue reading below.
Getting the Data
In this example, the data is coming from a simple service class that simulates fetching data from a database:
Source code
BufferedGridView.java
In a real world application, the service would be in its own file. The data would be loaded from a database and the result size limited to avoid flooding the UI with too many items. For the same reason an empty filter string returns no items in this example.
|
Note
|
Can you call a repository or Data Access Object (DAO) directly from the UI?
Yes, if you’re not using method security or any other service layer logic.
|
Populating and Filtering the Grid
Since the filtering happens in the service, you need to call the service whenever the filter string changes, and then call setItems() on the grid with the returned items:
Source code
BufferedGridView.java
There is one caveat with this approach: selection is not preserved between calls to setItems(). If you need to preserve selection, consider using a paginated grid instead.
Sorting
Sorting happens in the grid automatically, for every column that is marked as sortable:
Source code
BufferedGridView.java
For more information about sorting, see the Grid component documentation.