Skip to content

Commit 2c21473

Browse files
committed
Make it possible to control how filtering is applied to Grid
Fix #32
1 parent 32b18f0 commit 2c21473

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 2.0.1
2+
* Make it possible to control how filtering is applied to Grid #32
3+
* Updated dependencies
4+
15
# 2.0.0
26
_Reworked component_
37

vaadin-grid-filter/src/main/java/software/xdev/vaadin/gridfilter/GridFilter.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
import java.util.Map;
3030
import java.util.Objects;
3131
import java.util.Set;
32+
import java.util.function.BiConsumer;
3233
import java.util.function.Function;
34+
import java.util.function.Predicate;
3335
import java.util.stream.Collectors;
3436

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

105+
/**
106+
* A function that defines how the filter is applied to the {@link Grid}.
107+
* <p>
108+
* Please note that the default will only work for {@link com.vaadin.flow.data.provider.ListDataProvider} or
109+
* derivates
110+
* </p>
111+
*/
112+
protected BiConsumer<Grid<T>, List<FilterComponent<T, ?>>> applyFilter = (gr, components)
113+
-> gr.getListDataView().setFilter(item -> components.stream().allMatch(c -> c.test(item)));
114+
103115
public GridFilter(final Grid<T> grid)
104116
{
105117
this.grid = grid;
@@ -112,6 +124,26 @@ public GridFilter(final Grid<T> grid)
112124
this.addClassNames(GridFilterStyles.GRID_FILTER);
113125
}
114126

127+
/**
128+
* @see #applyFilter
129+
*/
130+
public GridFilter<T> withApplyPredicateFilter(final BiConsumer<Grid<T>, Predicate<T>> applyPredicateFilter)
131+
{
132+
Objects.requireNonNull(applyPredicateFilter);
133+
this.applyFilter = (gr, components)
134+
-> applyPredicateFilter.accept(gr, item -> components.stream().allMatch(c -> c.test(item)));
135+
return this;
136+
}
137+
138+
/**
139+
* @see #applyFilter
140+
*/
141+
public GridFilter<T> withApplyFilter(final BiConsumer<Grid<T>, List<FilterComponent<T, ?>>> applyFilter)
142+
{
143+
this.applyFilter = Objects.requireNonNull(applyFilter);
144+
return this;
145+
}
146+
115147
@SuppressWarnings("java:S1452")
116148
public FilterComponent<T, ?> addFilterComponent(final FilterComponentSupplier supplier)
117149
{
@@ -130,12 +162,16 @@ public GridFilter(final Grid<T> grid)
130162

131163
protected void onFilterUpdate()
132164
{
133-
this.grid.getListDataView().setFilter(item ->
134-
this.filterContainerComponent.getFilterComponents().stream().allMatch(c -> c.test(item)));
165+
this.applyFilterToGrid();
135166

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

170+
protected void applyFilterToGrid()
171+
{
172+
this.applyFilter.accept(this.grid, this.filterContainerComponent.getFilterComponents());
173+
}
174+
139175
@SuppressWarnings({"unchecked", "rawtypes"})
140176
public Registration addFilterChangedListener(final ComponentEventListener<FilterChangedEvent<T>> listener)
141177
{

0 commit comments

Comments
 (0)