diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c22ab8..f5dd3f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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_ diff --git a/vaadin-grid-filter/src/main/java/software/xdev/vaadin/gridfilter/GridFilter.java b/vaadin-grid-filter/src/main/java/software/xdev/vaadin/gridfilter/GridFilter.java index 75c3d96..6880e71 100644 --- a/vaadin-grid-filter/src/main/java/software/xdev/vaadin/gridfilter/GridFilter.java +++ b/vaadin-grid-filter/src/main/java/software/xdev/vaadin/gridfilter/GridFilter.java @@ -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; @@ -100,6 +102,16 @@ public class GridFilter new FilterContainerComponent<>(this::onFilterUpdate, false); protected final AddFilterComponentsButtons addFilterComponentButtons = new AddFilterComponentsButtons(); + /** + * A function that defines how the filter is applied to the {@link Grid}. + *

+ * Please note that the default will only work for {@link com.vaadin.flow.data.provider.ListDataProvider} or + * derivates + *

+ */ + protected BiConsumer, List>> applyFilter = (gr, components) + -> gr.getListDataView().setFilter(item -> components.stream().allMatch(c -> c.test(item))); + public GridFilter(final Grid grid) { this.grid = grid; @@ -112,6 +124,26 @@ public GridFilter(final Grid grid) this.addClassNames(GridFilterStyles.GRID_FILTER); } + /** + * @see #applyFilter + */ + public GridFilter withApplyPredicateFilter(final BiConsumer, Predicate> 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 withApplyFilter(final BiConsumer, List>> applyFilter) + { + this.applyFilter = Objects.requireNonNull(applyFilter); + return this; + } + @SuppressWarnings("java:S1452") public FilterComponent addFilterComponent(final FilterComponentSupplier supplier) { @@ -130,12 +162,16 @@ public GridFilter(final Grid 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> listener) {