Skip to content

Commit

Permalink
Fix #6953: DataTable global filterFunction (#6954)
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware committed Feb 6, 2021
1 parent 5da88b0 commit 36b2fe4
Showing 1 changed file with 12 additions and 4 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -138,16 +140,22 @@ public void filter(FacesContext context, DataTable table) {
ELContext elContext = context.getELContext();
Map<String, FilterMeta> 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();
AtomicBoolean globalMatch = new AtomicBoolean();

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;
Expand All @@ -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));
Expand All @@ -182,7 +190,7 @@ public void filter(FacesContext context, DataTable table) {
}

if (matches) {
filtered.add(table.getRowData());
filtered.add(rowData);
}
}

Expand Down

0 comments on commit 36b2fe4

Please sign in to comment.