29
29
import java .util .Map ;
30
30
import java .util .Objects ;
31
31
import java .util .Set ;
32
+ import java .util .function .BiConsumer ;
32
33
import java .util .function .Function ;
34
+ import java .util .function .Predicate ;
33
35
import java .util .stream .Collectors ;
34
36
35
37
import com .vaadin .flow .component .AttachEvent ;
@@ -100,6 +102,16 @@ public class GridFilter<T>
100
102
new FilterContainerComponent <>(this ::onFilterUpdate , false );
101
103
protected final AddFilterComponentsButtons addFilterComponentButtons = new AddFilterComponentsButtons ();
102
104
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
+
103
115
public GridFilter (final Grid <T > grid )
104
116
{
105
117
this .grid = grid ;
@@ -112,6 +124,26 @@ public GridFilter(final Grid<T> grid)
112
124
this .addClassNames (GridFilterStyles .GRID_FILTER );
113
125
}
114
126
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
+
115
147
@ SuppressWarnings ("java:S1452" )
116
148
public FilterComponent <T , ?> addFilterComponent (final FilterComponentSupplier supplier )
117
149
{
@@ -130,12 +162,16 @@ public GridFilter(final Grid<T> grid)
130
162
131
163
protected void onFilterUpdate ()
132
164
{
133
- this .grid .getListDataView ().setFilter (item ->
134
- this .filterContainerComponent .getFilterComponents ().stream ().allMatch (c -> c .test (item )));
165
+ this .applyFilterToGrid ();
135
166
136
167
this .fireEvent (new FilterChangedEvent <>(this , false ));
137
168
}
138
169
170
+ protected void applyFilterToGrid ()
171
+ {
172
+ this .applyFilter .accept (this .grid , this .filterContainerComponent .getFilterComponents ());
173
+ }
174
+
139
175
@ SuppressWarnings ({"unchecked" , "rawtypes" })
140
176
public Registration addFilterChangedListener (final ComponentEventListener <FilterChangedEvent <T >> listener )
141
177
{
0 commit comments