DataTable: custom filter icon is not updating
Describe the bug
Filter Icon button color is not changing when custom filter is applied, to show filter has been applied. It changes automatically in case of default filters.
Reproducer
https://codesandbox.io/s/loving-bush-5gr5y4?file=/src/demo
PrimeReact version
8.1.1
React version
17.x
Language
ES6
Build / Runtime
Create React App (CRA)
Browser(s)
Chrome
Steps to reproduce the behavior
- On name column click filter icon
- Filter window opens. From second dropdown select custom filter (My Filter)
- Click on apply button.
- Color didn't change. (Refer below image)
Expected behavior
Color should have been changed (light blue color refer below image) as it changes when default filters are applied.

I'm having the same issue. What I've figured out is the "p-column-filter-menu-button-active" class is not getting added to the button tag.
The issue appears to be with the filter and onFilter properties on the DataTable itself. The only way I can get the indicator to work is by passing in a filters={filters} but NOT an onFilter={(e) => setFilters(e.filters)}
I stripped out a lot of unnecessary stuff from the sample to try to replicate. As it stands, it's a strange hybrid of controlled (filters) and uncontrolled (missing onFilter to update the React state). This hybrid works, but it's the only hybrid that appears to work.
If you comment out "filters" on line 83 the indicator stops working.
If you uncomment both lines 83 and 84 (filters and onFilter), the indicator also stops working.
Additionally, if you use the hybrid of filters but not onFilter but you do not initialize the filter object by commenting out line 31, the indicator also stops working.
Summary, the only combination in which the indicator works is:
- Create a filters object with default filters
- set filters on the dataTable to use the filters object
- do NOT use onFilter to update the object
https://codesandbox.io/s/winter-sun-s3mddf?file=/src/demo/DataTableFilterDemo.js
Yes, I guess its bug
Problem in there
https://github.com/primefaces/primereact/blob/46e5ae66d656971f4c538e4a22d3d0a9dc9cd3b4/components/lib/datatable/ColumnFilter.js#L35
They sets p-column-filter-menu-button-active class if current filterState has difference old state. Your code always updates filterState so does not set highlights
solution :
check filterModel.value is not null
@omansak care to submit a PR?
I fixed it with my PR.
Unfortunately this issue is not fixed. Just tried PrimeReact 8.4.0 but datatable filter icon not highlighting when filter value selected.
Filter icon highlighting functions correctly for hooks but not class component.
We are stuck using PrimeReact 7.2.1 until this issue is fixed since we have many class datatable components.
Can this issue be looked into again?
hmm its working for me. Can you provide a code sandbox reproducer showing its not working on 8.4.0 please?
Oh I see try this sandbox on 8.4.0: https://codesandbox.io/s/loving-bush-5gr5y4?file=/src/demo
it works if you pick My Custom and enter a value in the text box. But if you don't enter a value in the text box the filter thinks its not filtering anything. So as long a a filter HAS a value it highlights.
The sandbox you provided is using hooks which we agree does work. The issue occurs when using class, e.g.,
class Customers extends React.Component { .... }
Can you provide a sandbox that uses class?
OK so you are still on 7.X branch which is no longer being patched. The only way you will receive support on 7.X i assume is through PRO support:
To clarify, the issue of the filter icon highlight is in 8.X. The filter is not highlighting when a value is selected for datatable class components. This is why we are stuck on 7.X until it is fixed in 8.X.
Are you able to provide a sandbox of a datatable class component with filters using 8.X?
no but if you can provide a working sandbox showing the issue I can look at it
The issue with the onFilter is still happening.
When onFilter is set in the datatable, the p-column-filter-menu-button-active class is not applied to the filter icon.
I forked the sandbox mentioned above and created a second datatable in it, almost identical to the first datatable, except that it sets the onFilter function.
Check it out: https://codesandbox.io/s/custom-on-filter-not-applying-active-state-on-icon-fdrgr8
The filter icon on the first datatable has its color changed to blue, showing that it's active:

But the second datatable doesn't change at all:

feel free to submit a PR for review
According to my research when you use datable in controlled mode (assign/update filters prop manually) the filterModel.constraints and filterStoreModel.constraints are identical.
I didn't test it but it seems the problem may be in https://github.com/primefaces/primereact/blob/master/components/lib/datatable/ColumnFilter.js line 37. as omansak mentioned before: omansak commented on 17 Jun
There is a check hasFIlter: https://github.com/primefaces/primereact/blob/9b52d8588b6b276c0842421065b179c17849193f/components/lib/datatable/ColumnFilter.js#L37
const hasFilter = () => {
if (!filterStoreModel || !filterModel) return false;
return filterStoreModel.operator
? !isFilterBlank(filterModel.constraints[0].value) && filterStoreModel.constraints[0].value !== filterModel.constraints[0].value
: !isFilterBlank(filterModel.value) && filterStoreModel.value !== filterModel.value;
};
this will be false in controlled mode when filterModel.constraints and filterStoreModel.constraints are identical
filterStoreModel.constraints[0].value !== filterModel.constraints[0].value
That's great information. Any ideas how to add a "work around" so the filter icon will highlight? Below is sample code of how we use datatable in control mode:
constructor(props) {
...
this.doFetchCustomersThrottled = _.debounce(this.doFetchCustomers, 300);
...
}
onFilter = (event) => {
saveToSS(CUSTOMER_TABLE_FILTERS, event.filters); this.setState({ filters: event.filters, customerNumberSearch: event?.filters?.number?.value, customerNameSearch: event?.filters?.name?.value, customerLocationSearch: event?.filters?.location?.value, customerContactSearch: event?.filters?.contact?.value, selectedCustomerTypes: event?.filters?.cusType?.value || [], selectedReferralTypes: event?.filters?.referral?.value || [], selectedSalesReps: event?.filters?.salesRep?.value || [], }, callback () => this.doFetchCustomersThrottled(this.props.pagination, this.props.sorting));
}
return (
<DataTable>
...
onFilter={this.onFilter}
...
</DataTable>
)
Unfortunately I don't know any way to make a workaround :(, AFAIK filterStoreModel is managed internally by DataTable. We have been searching for a solution ourselves with no luck. @melloware maybe you can shed some light on why hasFIlter can not have just: const hasFilter = () => { if (!filterStoreModel || !filterModel) return false; return filterStoreModel.operator ? !isFilterBlank(filterModel.constraints[0].value) : !isFilterBlank(filterModel.value) ; };
without check: filterStoreModel.constraints[0].value !== filterModel.constraints[0].value ? Or how can we have the icon highlighted in controlled mode? I suppose there is a reason why this additional check is there, that I can't see ...
Thank you.
+1 encountering this bug myself. Hoping for a fix
I am not quite sure why it behaves that way. I suggest if you guys need more support to look into PRO support I am just an open source volunteer.
Should I submit a "new issue" here on GitHub for a bug fix and reference #3052?
Hi Melloware, I can submit PR with this change
const hasFilter = () => { if (!filterStoreModel || !filterModel) return false; return filterStoreModel.operator ? !isFilterBlank(filterModel.constraints[0].value) : !isFilterBlank(filterModel.value) ; };
I just hoped someone who put the additional checks can explain why these were necessary. Maybe removing can cause other issues ...
wt., 20 wrz 2022, 15:21 użytkownik Bella FSM @.***> napisał:
Should I submit a "new issue" here on GitHub for a bug fix and reference #3052 https://github.com/primefaces/primereact/pull/3052?
— Reply to this email directly, view it on GitHub https://github.com/primefaces/primereact/issues/2976#issuecomment-1252346884, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJDRXYLRJKAW4BJM7NQ6ZW3V7G26DANCNFSM5YG5UH3Q . You are receiving this because you commented.Message ID: @.***>