From 36b2fe415b1a7fb9600b368228c5f9fb5d617f69 Mon Sep 17 00:00:00 2001 From: Melloware Date: Sat, 6 Feb 2021 12:50:56 -0500 Subject: [PATCH] Fix #6953: DataTable global filterFunction (#6954) --- .../datatable/feature/FilterFeature.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/primefaces/component/datatable/feature/FilterFeature.java b/src/main/java/org/primefaces/component/datatable/feature/FilterFeature.java index 6d0ff83ec0..d0b4584396 100644 --- a/src/main/java/org/primefaces/component/datatable/feature/FilterFeature.java +++ b/src/main/java/org/primefaces/component/datatable/feature/FilterFeature.java @@ -34,6 +34,7 @@ import org.primefaces.util.MapBuilder; import javax.el.ELContext; +import javax.el.MethodExpression; import javax.faces.context.FacesContext; import javax.faces.event.PhaseId; import java.io.IOException; @@ -89,6 +90,7 @@ public void decode(FacesContext context, DataTable table) { table.setRows(Integer.parseInt(rppValue)); } + if (table.isLazy()) { if (table.isLiveScroll()) { table.loadLazyScrollData(0, table.getScrollRows()); @@ -138,6 +140,9 @@ public void filter(FacesContext context, DataTable table) { ELContext elContext = context.getELContext(); Map filterBy = table.getFilterByAsMap(); FilterMeta globalFilter = filterBy.get(FilterMeta.GLOBAL_FILTER_KEY); + MethodExpression globalFilterFunction = table.getGlobalFilterFunction(); + boolean hasGlobalFilterFunction = globalFilterFunction != null && globalFilter != null; + table.setValue(null); // reset value (instead of filtering on already filtered value) AtomicBoolean localMatch = new AtomicBoolean(); @@ -145,9 +150,12 @@ public void filter(FacesContext context, DataTable table) { for (int i = 0; i < table.getRowCount(); i++) { table.setRowIndex(i); + Object rowData = table.getRowData(); localMatch.set(true); - if (globalFilter != null) { - globalMatch.set(false); + globalMatch.set(false); + + if (hasGlobalFilterFunction) { + globalMatch.set((Boolean) globalFilterFunction.invoke(elContext, new Object[]{rowData, globalFilter.getFilterValue(), filterLocale})); } final int rowIndex = i; @@ -159,7 +167,7 @@ public void filter(FacesContext context, DataTable table) { Object columnValue = filter.getLocalValue(elContext); - if (globalFilter != null && !globalMatch.get()) { + if (globalFilter != null && !globalMatch.get() && !hasGlobalFilterFunction) { FilterConstraint constraint = globalFilter.getConstraint(); Object filterValue = globalFilter.getFilterValue(); globalMatch.set(constraint.isMatching(context, columnValue, filterValue, filterLocale)); @@ -182,7 +190,7 @@ public void filter(FacesContext context, DataTable table) { } if (matches) { - filtered.add(table.getRowData()); + filtered.add(rowData); } }