Skip to content

Commit

Permalink
Make it possible to control how filtering is applied to Grid
Browse files Browse the repository at this point in the history
Fix #32
  • Loading branch information
AB-xdev committed Nov 18, 2024
1 parent 32b18f0 commit 2c21473
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 2.0.1
* Make it possible to control how filtering is applied to Grid #32
* Updated dependencies

# 2.0.0
_Reworked component_

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import com.vaadin.flow.component.AttachEvent;
Expand Down Expand Up @@ -100,6 +102,16 @@ public class GridFilter<T>
new FilterContainerComponent<>(this::onFilterUpdate, false);
protected final AddFilterComponentsButtons addFilterComponentButtons = new AddFilterComponentsButtons();

/**
* A function that defines how the filter is applied to the {@link Grid}.
* <p>
* Please note that the default will only work for {@link com.vaadin.flow.data.provider.ListDataProvider} or
* derivates
* </p>
*/
protected BiConsumer<Grid<T>, List<FilterComponent<T, ?>>> applyFilter = (gr, components)
-> gr.getListDataView().setFilter(item -> components.stream().allMatch(c -> c.test(item)));

public GridFilter(final Grid<T> grid)
{
this.grid = grid;
Expand All @@ -112,6 +124,26 @@ public GridFilter(final Grid<T> grid)
this.addClassNames(GridFilterStyles.GRID_FILTER);
}

/**
* @see #applyFilter
*/
public GridFilter<T> withApplyPredicateFilter(final BiConsumer<Grid<T>, Predicate<T>> applyPredicateFilter)
{
Objects.requireNonNull(applyPredicateFilter);
this.applyFilter = (gr, components)
-> applyPredicateFilter.accept(gr, item -> components.stream().allMatch(c -> c.test(item)));
return this;
}

/**
* @see #applyFilter
*/
public GridFilter<T> withApplyFilter(final BiConsumer<Grid<T>, List<FilterComponent<T, ?>>> applyFilter)
{
this.applyFilter = Objects.requireNonNull(applyFilter);
return this;
}

@SuppressWarnings("java:S1452")
public FilterComponent<T, ?> addFilterComponent(final FilterComponentSupplier supplier)
{
Expand All @@ -130,12 +162,16 @@ public GridFilter(final Grid<T> grid)

protected void onFilterUpdate()
{
this.grid.getListDataView().setFilter(item ->
this.filterContainerComponent.getFilterComponents().stream().allMatch(c -> c.test(item)));
this.applyFilterToGrid();

this.fireEvent(new FilterChangedEvent<>(this, false));
}

protected void applyFilterToGrid()
{
this.applyFilter.accept(this.grid, this.filterContainerComponent.getFilterComponents());
}

@SuppressWarnings({"unchecked", "rawtypes"})
public Registration addFilterChangedListener(final ComponentEventListener<FilterChangedEvent<T>> listener)
{
Expand Down

0 comments on commit 2c21473

Please sign in to comment.