Skip to content

Commit c54b7e3

Browse files
authored
Set items to ListDataView directly (#4973)
* Set items to ListDataView directly * Update explanation for filtering and selection handling Clarify the reason for using ListDataView.setItems() over Grid.setItems() to preserve selection.
1 parent 31313dc commit c54b7e3

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

articles/building-apps/forms-data/add-grid/buffered-data.adoc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,14 @@ Yes, if you're not using <<../../security/protect-services#,method security>> or
4646

4747
== Populating and Filtering the Grid
4848

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:
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's `ListDataView` with the returned items:
5050

5151
[source,java]
5252
----
5353
include::{root}/src/main/java/com/vaadin/demo/buildingapps/grid/BufferedGridView.java[tags=filtering,indent=0]
5454
----
5555

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
56+
The reason you're calling `ListDataView.setItems()` and not `Grid.setItems()` is that `Grid.setItems()` always creates a new data provider. This in turns clears the selection of the grid. If you want to preserve the selection, you need to update the existing data provider and that is what `ListDataView.setItems()` does.
5857

5958

6059
== Sorting

src/main/java/com/vaadin/demo/buildingapps/grid/BufferedGridView.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public class BufferedGridView extends VerticalLayout {
3232

3333
// tag::filtering[]
3434
// Update the grid whenever the text field changes
35-
filterField.addValueChangeListener(e -> grid.setItems(
36-
service.findItems(e.getValue())));
35+
filterField.addValueChangeListener(e -> grid.getListDataView()
36+
.setItems(service.findItems(e.getValue())));
3737
// end::filtering[]
3838

3939
// Layout components

0 commit comments

Comments
 (0)