Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Ensure that GridListDataView#setFilter fires DataChangeEvent #5304

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5039,7 +5039,8 @@ private void onInMemoryFilterOrSortingChange(
updateInMemorySorting(sortComparator);
updateInMemoryFiltering(filter);

dataCommunicator.reset();
// Call refreshAll as data may have changed due filter change
getDataProvider().refreshAll();
Copy link
Contributor

@mshabarov mshabarov Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose to move refreshAll() from here to the data view.
Then the setFilter on the Flow side will be like this:

    @Override
    public AbstractListDataView<T> setFilter(SerializablePredicate<T> filter) {
        DataViewUtils.setComponentFilter(component, filter);
        fireFilteringOrSortingChangeEvent(filter,
                (SerializableComparator<T>) DataViewUtils
                        .getComponentSortComparator(component).orElse(null));
        refreshAll();
        return this;
    }

This would fix all similar issues in data view, not only the Grid.

}

private void updateInMemoryFiltering(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.vaadin.flow.component.grid.dataview;

import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

import org.junit.Assert;
Expand Down Expand Up @@ -111,11 +112,17 @@ public void dataViewWithItems_returnsExpectedItemsForMethods() {

@Test
public void dataView_setFilter_methodsUseFilteredData() {
AtomicInteger refreshed = new AtomicInteger(0);
String[] items = new String[] { "item1", "item2", "item3", "item4" };
Grid<String> grid = new Grid<>();
GridListDataView<String> dataView = grid.setItems(items);
grid.getDataProvider().addDataProviderListener(e -> {
refreshed.incrementAndGet();
});

dataView.setFilter(s -> s.endsWith("4"));
Assert.assertEquals("Filter change did not fire DataChangeEvent", 1,
refreshed.get());

Assert.assertEquals("Filter was not applied to data size", 1,
dataView.getItemCount());
Expand Down