Skip to content

Commit 22a2d5c

Browse files
authored
Edit "Views and Navigation" section in Building Apps (#4842)
* Edit Add a View guide * Edit Navigate to a View * Edit Route Parameters * Edit Route Templates * Edit Query Parameters (WIP) * Add Query Parameters example * Restructure * Add Navigation Menu guide * WIP Master-Detail * Master-Detail View guide ready * Tweaks * WIP static data * WIP * Change copy-paste example to section * Tweak line breaks to remove horizontal scrolling
1 parent b600b10 commit 22a2d5c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1959
-2661
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
title: Buffered Data
3+
page-title: How to add a buffered grid to a Vaadin application
4+
description: Learn how to populate a grid with data from an application service.
5+
meta-description: Learn how to populate a grid with data from an application service.
6+
order: 20
7+
---
8+
9+
= Add a Buffered Grid
10+
11+
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).
12+
13+
As with <<static-data#,static data>>, *buffered data is loaded into the grid at once and sorted in memory*. However, *the filtering happens in the application service*.
14+
15+
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 <</components/grid#,Grid>> component documentation.
16+
17+
18+
== Copy-Paste into Your Project
19+
20+
If you want to quickly try out a buffered grid in your Vaadin application, copy-paste the following code into your project:
21+
22+
[source,java]
23+
----
24+
include::{root}/src/main/java/com/vaadin/demo/buildingapps/grid/BufferedGridView.java[tags=full,indent=0]
25+
----
26+
27+
For more detailed walk-through of the example code, continue reading below.
28+
29+
30+
== Getting the Data
31+
32+
In this example, the data is coming from a simple service class that _simulates_ fetching data from a database:
33+
34+
[source,java]
35+
----
36+
include::{root}/src/main/java/com/vaadin/demo/buildingapps/grid/BufferedGridView.java[tags=data,indent=0]
37+
----
38+
39+
In a real world application, the service would be in its own file. The data would be loaded from a database
40+
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.
41+
42+
.Can you call a repository or Data Access Object (DAO) directly from the UI?
43+
[NOTE]
44+
Yes, if you're not using <<../../security/protect-services#,method security>> or any other service layer logic.
45+
46+
47+
== Populating and Filtering the Grid
48+
49+
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:
50+
51+
[source,java]
52+
----
53+
include::{root}/src/main/java/com/vaadin/demo/buildingapps/grid/BufferedGridView.java[tags=filtering,indent=0]
54+
----
55+
56+
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-data#,paginated grid>> instead.
57+
// TODO Add link to guide about selection handling in grids
58+
59+
60+
== Sorting
61+
62+
Sorting happens in the grid automatically, for every column that is *marked as sortable*:
63+
64+
[source,java]
65+
----
66+
include::{root}/src/main/java/com/vaadin/demo/buildingapps/grid/BufferedGridView.java[tags=sorting,indent=0]
67+
----
68+
69+
For more information about sorting, see the <</components/grid#sorting,Grid>> component documentation.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: Add a Grid
3+
page-title: How to add a data grid to a Vaadin view
4+
description: Learn how to add a data grid to a Vaadin view.
5+
meta-description: Learn how to add a data grid to a Vaadin view.
6+
order: 10
7+
---
8+
9+
= Add a Grid
10+
11+
In business applications, grids are commonly used to display tabular data. Vaadin's <</components/grid#,Grid>> component provides a powerful and flexible way to present large datasets. However, the way you populate, filter, and sort the grid depends on the nature of your data.
12+
13+
Data in business applications typically falls into one of the following categories:
14+
15+
* *Static data*: A fixed collection of objects held entirely in memory that never changes. This is typically reference data (for example, countries, categories, status types) that is either hardcoded or loaded once at application startup. Java `enum` constants also fall into this category. Filtering and sorting happen entirely in memory.
16+
17+
* *Buffered data*: Data that is loaded from an application service where the entire filtered result set is fetched at once. Because all matching records are held in memory, sorting can happen locally without additional service calls. 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).
18+
19+
* *Paginated data*: Data that is too large to fit in memory, even after filtering. It is loaded in chunks (pages) from an application service as needed. Both filtering and sorting are delegated to the service layer.
20+
21+
You can use the Grid component with all these data types, but the implementation details differ. Each data type has its own guide:
22+
23+
section_outline::[]
24+
25+
For detailed information about binding data sets to UI components in Vaadin, see the <</flow/binding-data/data-provider#,Data Provider>> reference guide.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
---
2+
title: Paginated Data
3+
page-title: How to add a paginated grid to a Vaadin application
4+
description: Learn how to populate a grid with paginated data from an application service.
5+
meta-description: Learn how to populate a grid with paginated data from an application service.
6+
order: 30
7+
---
8+
9+
= Add a Paginated Grid
10+
:toclevels: 2
11+
12+
Paginated data refers to datasets that are too large to fit in memory, even after filtering. Instead, *the data is loaded in chunks (pages)* from an application service as needed. *Both filtering and sorting are delegated to the service layer* (typically a database).
13+
14+
In Vaadin, the Grid component has built-in support for paginated data (often referred to as lazy loading in API documentation). When the user scrolls or changes the sorting/filtering criteria, the grid requests the appropriate data page from the backend service. To the user, this appears as a seamless scrolling experience.
15+
16+
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 <</components/grid#,Grid>> component documentation.
17+
18+
19+
== Copy-Paste into Your Project
20+
21+
If you want to quickly try out a paginated grid in your Vaadin application, copy-paste the following code into your project:
22+
23+
[source,java]
24+
----
25+
include::{root}/src/main/java/com/vaadin/demo/buildingapps/grid/PaginatedGridView.java[tags=full,indent=0]
26+
----
27+
28+
For more detailed walk-through of the example code, continue reading below.
29+
30+
31+
== Getting the Data
32+
33+
The Grid component can request pages either by using a page number and size, or by using an offset and limit. If the dataset is sorted, you have to sort it before applying pagination.
34+
35+
This example uses offset-based pagination and uses Vaadin's `QuerySortOrder` API to pass sorting information to the application service:
36+
37+
[source,java]
38+
----
39+
include::{root}/src/main/java/com/vaadin/demo/buildingapps/grid/PaginatedGridView.java[tags=data,indent=0]
40+
----
41+
42+
In a real world application, the service would be in its own file and the data would be queried from a database.
43+
44+
45+
== Populating the Grid
46+
47+
You populate the grid with paginated data by passing it a callback function that fetches data pages on demand. The callback function receives a `Query` object that contains information about the requested page, sorting, and filtering criteria.
48+
49+
[source,java]
50+
----
51+
include::{root}/src/main/java/com/vaadin/demo/buildingapps/grid/PaginatedGridView.java[tags=dataprovider,indent=0]
52+
----
53+
54+
55+
=== Spring Data Support
56+
57+
If you are using Spring Data, use the `setItemsPageable()` method. It passes a `Pageable` object to the callback function that you can use directly with <<../repositories/jpa#,Spring Data repositories>>:
58+
59+
[source,java]
60+
----
61+
// Spring Data Repository:
62+
public interface ItemReposiory<Item, Long> extends PagingAndSortingRepository<Item, Long> {
63+
Slice<Item> findByNameContainingIgnoreCase(String name, Pageable pageable);
64+
}
65+
66+
// In the view class:
67+
grid.setItemsPageable(pageable -> repository
68+
.findByNameContainingIgnoreCase(filterField.getValue(), pageable)
69+
.getContent()
70+
);
71+
----
72+
73+
.Can you call a repository directly from the UI?
74+
[NOTE]
75+
Yes, if you're not using <<../../security/protect-services#,method security>> or any other service layer logic.
76+
Otherwise, it's better to have an <<../../business-logic/add-service#,application service>> between the UI and the repository.
77+
78+
79+
== Filtering
80+
81+
The filtering happens in the application service and the filter string is passed to the service via the callback function. Because of this, you have to refresh the grid whenever the filter string changes:
82+
83+
[source,java]
84+
----
85+
include::{root}/src/main/java/com/vaadin/demo/buildingapps/grid/PaginatedGridView.java[tags=filtering,indent=0]
86+
----
87+
88+
89+
== Sorting
90+
91+
Sorting happens in the application service and the sort orders are passed to the service via the callback function. Only columns with a specific *sort property* are sortable:
92+
93+
[source,java]
94+
----
95+
include::{root}/src/main/java/com/vaadin/demo/buildingapps/grid/PaginatedGridView.java[tags=sorting,indent=0]
96+
----
97+
98+
Sort properties are strings that are passed to the application service. They typically correspond to database column names, but in this example they are mapped to the record fields in the `Item` class.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
title: Static Data
3+
page-title: How to add a static grid to a Vaadin application
4+
description: Learn how to populate a grid with static data.
5+
meta-description: Learn how to populate a grid with static data in a Vaadin application.
6+
order: 10
7+
---
8+
9+
= Add a Static Grid
10+
11+
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.
12+
13+
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*.
14+
15+
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 <</components/grid#,Grid>> component documentation.
16+
17+
18+
== Copy-Paste into Your Project
19+
20+
If you want to quickly try out a static grid in your Vaadin application, copy-paste the following code into your project:
21+
22+
[source,java]
23+
----
24+
include::{root}/src/main/java/com/vaadin/demo/buildingapps/grid/StaticGridView.java[tags=full,indent=0]
25+
----
26+
27+
For more detailed walk-through of the example code, continue reading below.
28+
29+
30+
== Getting the Data
31+
32+
In this example, the static data is a list of record objects returned by a simple service class:
33+
34+
[source,java]
35+
----
36+
include::{root}/src/main/java/com/vaadin/demo/buildingapps/grid/StaticGridView.java[tags=data,indent=0]
37+
----
38+
39+
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.
40+
41+
42+
== Populating the Grid
43+
44+
You populate the grid with static data by passing the collection of items to the grid's `setItems()` method:
45+
46+
[source,java]
47+
----
48+
include::{root}/src/main/java/com/vaadin/demo/buildingapps/grid/StaticGridView.java[tags=dataprovider,indent=0]
49+
----
50+
51+
The method returns a `ListDataView` object that you need if you want to filter the data. This is covered in the next section.
52+
53+
54+
== Filtering
55+
56+
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.
57+
58+
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:
59+
60+
[source,java]
61+
----
62+
include::{root}/src/main/java/com/vaadin/demo/buildingapps/grid/StaticGridView.java[tags=filtering,indent=0]
63+
----
64+
65+
[NOTE]
66+
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.
67+
68+
For more information about filtering, see the <</components/grid#filtering,Grid>> component documentation.
69+
70+
== Sorting
71+
72+
Sorting happens in the grid automatically, for every column that is *marked as sortable*:
73+
74+
[source,java]
75+
----
76+
include::{root}/src/main/java/com/vaadin/demo/buildingapps/grid/StaticGridView.java[tags=sorting,indent=0]
77+
----
78+
79+
For more information about sorting, see the <</components/grid#sorting,Grid>> component documentation.

0 commit comments

Comments
 (0)