You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/components/grid/data-binding.adoc
+16-50Lines changed: 16 additions & 50 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,13 +9,13 @@ section-nav: badge-flow
9
9
= Binding Data to Grid [badge-flow]#Flow#
10
10
:toclevels: 2
11
11
12
-
Grid supports connecting to various types of data sources through data providers. A data provider is a class that implements the [interfacename]`DataProvider` interface and encapsulates the logic for fetching items from a specific data source, with support for pagination, sorting, and filtering. Grid provides convenience methods to simplify common binding cases where a full data provider setup isn't needed, and also supports fully custom data providers for more complex scenarios.
12
+
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.
13
13
14
14
== Binding Items via `setItems`
15
15
16
-
The simplest way to bind data to a Grid is by using the `setItems` convenience method. It lets you bind an in-memory collection or provide callbacks for lazy loading from a backend service without explicitly creating a data provider.
16
+
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.
17
17
18
-
*Example:* Binding items from an in-memory collection.
18
+
The example below shows how to display, filter and sort a static list of `Person` records:
19
19
20
20
[.example]
21
21
--
@@ -71,7 +71,7 @@ public record Person(String name) {
71
71
72
72
--
73
73
74
-
*Example:* Binding items from a data service via callbacks.
74
+
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:
75
75
76
76
[.example]
77
77
--
@@ -165,66 +165,29 @@ More documentation and examples for using the `setItems` method are available in
165
165
166
166
== Custom Data Providers
167
167
168
-
Alternatively, you can supply a custom data provider via the `setDataProvider(DataProvider<T, ?>)` method, passing your own class that implements the [interfacename]`DataProvider` interface:
168
+
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.
169
169
170
-
[source,java]
171
-
----
172
-
grid.setDataProvider(new CustomDataProvider());
173
-
----
174
-
175
-
For convenience, Flow provides an abstract base class `AbstractDataProvider`, which you can extend to create a custom data provider. This class requires implementing the following methods:
176
-
177
-
[source,java]
178
-
----
179
-
public class CustomDataProvider extends AbstractDataProvider<T, F> {
180
-
@Override
181
-
public boolean isInMemory() { // <1>
182
-
// Your implementation here
183
-
}
184
-
185
-
@Override
186
-
public int size(Query<T, F> query) { // <2>
187
-
// Your implementation here
188
-
}
189
-
190
-
@Override
191
-
public Stream<T> fetch(Query<T, F> query) { // <3>
192
-
// Your implementation here
193
-
}
194
-
}
195
-
----
196
-
<1> The [methodname]`isInMemory` method indicates whether the data provider uses an in-memory data source. When all data is held in memory, Grid can enable features like a determinate Select All checkbox, which is only available in this case.
197
-
<2> The [methodname]`size` method returns the number of items based on the query parameters.
198
-
<3> The [methodname]`fetch` method returns a stream of items based on the query parameters.
199
-
200
-
The `Query<T, F>` parameter in the `size` and `fetch` methods provides information about the requested data such as pagination details (offset and limit), sorting orders, and an optional filter object of type `F`.
201
-
202
-
Below is a practical example of a custom data provider that extends the `AbstractDataProvider` class and uses a fake database to return `Person` records. It supports filtering with a `PersonFilter` object and sorting using `QuerySortOrder` objects from the Grid. The example also includes a simple view that uses the data provider with a Grid and filter components:
170
+
The example below shows a Grid backed by a custom data provider that simulates fetching data from a database, with support for filtering and sorting:
203
171
204
172
[.example]
205
173
--
206
174
207
175
[source,java]
208
176
.PersonDataProvider.java
209
177
----
210
-
public class PersonDataProvider extends AbstractDataProvider<Person, PersonFilter> {
178
+
public class PersonDataProvider extends AbstractBackendDataProvider<Person, PersonFilter> {
211
179
// In a real application, data would come from a real database or backend service.
212
180
// This example uses a static list for demonstration purposes only.
213
-
private final List<Person> FAKE_DATABASE = Arrays.asList(
181
+
private final List<Person> DATABASE = Arrays.asList(
214
182
new Person("Michael Chen", "Engineering"),
215
183
new Person("Sarah Johnson", "Engineering"),
216
184
new Person("David Rodriguez", "Marketing"),
217
-
new Person("Emma Wilson", "HR"),
218
-
219
-
@Override
220
-
public boolean isInMemory() {
221
-
return false;
222
-
}
185
+
new Person("Emma Wilson", "HR"));
223
186
224
187
@Override
225
-
public Stream<Person> fetch(Query<Person, PersonFilter> query) {
@@ -346,3 +309,6 @@ public class PeopleView extends VerticalLayout {
346
309
----
347
310
348
311
--
312
+
313
+
More examples of custom data providers are available in the <<{articles}/flow/binding-data/data-provider#recycling-data-binding-logic,Binding Items To Components>> article.
You can create a separate data provider class. The following example uses only the [classname]`FetchCallBack`, but you can also implement a full data provider by extending [classname]`AbstractbackendDataProvider`.
583
+
To completely split off the lazy data binding logic from UI components, you can create a separate data provider class. The following example shows a data provider with only a fetch callback:
584
584
585
585
[source,java]
586
586
----
587
587
@SpringComponent
588
588
public class PersonDataProvider implements CallbackDataProvider.FetchCallback<Person, Void> {
589
589
590
590
@Autowired
591
-
PersonRepository repo;
591
+
private PersonRepository repo;
592
592
593
593
@Override
594
594
public Stream<Person> fetch(Query<Person, Void> query) {
@@ -598,7 +598,53 @@ public class PersonDataProvider implements CallbackDataProvider.FetchCallback<Pe
598
598
599
599
}
600
600
601
-
personGrid.setItems(dataProvider);
601
+
personGrid.setDataProvider(dataProvider);
602
+
----
603
+
604
+
You can also implement a full data provider by extending the [classname]`AbstractBackendDataProvider` class like so:
605
+
606
+
[source,java]
607
+
----
608
+
public class PersonDataProvider extends AbstractBackendDataProvider<Person, String> {
609
+
@Autowired
610
+
private PersonRepository repo;
611
+
612
+
@Override
613
+
protected int sizeInBackEnd(Query<Person, String> query) {
To use filtering with the data provider above, you need to instantiate it with a configurable filter type. This provides access to the [methodname]`setFilter()` method, which you can use to apply a filter value, as shown below:
631
+
632
+
[source,java]
633
+
----
634
+
// Create a data provider
635
+
PersonDataProvider dataProvider = new PersonDataProvider();
636
+
637
+
// Wrap the original data provider to support configurable filtering
0 commit comments