How to make custom DataTable filter update FilterMeta map in PrimeFaces 11

Viewed 213

We have a custom filter component for DataTable that works in PrimeFaces 8, but stopped to work in PF 11.

That custom filter updates the filter MatchMode with this call:

Column column = (Column) this.getParent();
column.setFilterMatchMode(this.filterMatchMode);

In PF 10 and 11 we have some changes in DataTable, but I don't know which one is responsible for filters map handling that caused above snippet stopped working.

First I think that some methods naming changes are related, so I tried to use new column.setFilterBy method, but DataTable do not reflect this in filters map.Then I put some break points on ColumnBase getters related to filtering, but none is called during filter.

So far, I think that I need to change filter directly in DataTable, but I have no clue how to.

2 Answers

This is what we do on our projects we have our own LazyDataTable implementation not sure if that makes a difference.

final Collection<FilterMeta> filters = table.getFilterMetaData();
filters.clear();

filters.add(FilterMeta.builder().field("registrationId").filterValue(registrationId).matchMode(MatchMode.EQUALS).build());

// construct the filter
table.setFilterMetaData(filters);

Using @Melloware answer as starting point, I managed to update filters map with this code:

Column column = (Column) this.getParent();
DataTable parentDataTable = (DataTable) column.getNamingContainer();
String key = column.getClientId();
// Get current filters map
Map<String, FilterMeta> filterByAsMap = parentDataTable.getFilterByAsMap();
// Get current column filter
FilterMeta filterMeta = filterByAsMap.get(key);
// Create new MatchMode and FilterConstraint
MatchMode matchMode = MatchMode.of(filterMatchMode);
FilterConstraint constraint = FilterFeature.FILTER_CONSTRAINTS.get(matchMode);
// Update column filter with MatchMode and FilterConstraint
filterMeta.setMatchMode(matchMode);
filterMeta.setConstraint(constraint);
// Update DataTable filters map
filterByAsMap.put(key, filterMeta);
parentDataTable.setFilterByAsMap(filterByAsMap);

Looks odd update filters map directly on DataTable, but current DataTable implementation do not cycle through columns to get filters like PF 8 did (AFAIK).

So... Don't know if this is correct or there are better approach, but for now this works.

A little note! My custom filter component handle value too, but filters map is correctly updated with new filter value without the need to this code above. It's probably handled by DataTable <-> Column <-> UIInput integration, if I can find how this works, maybe I can make a proper code to update filters map.

Related