Archive for September, 2016
(long tweet) When ‘filter’ does not work with Primefaces’ datatable
Abstract
Sometimes, the filter
function in Primefaces <p:datatable/>
does not work when the field on which filtering is operated typed as an enum
.
Explanation
Actually, in order to filter, Primefaces relies on a direct '='
comparison. The hack to fix this issue is to force Primefaces to compare on the enum name, and not by a reference check.
Quick fix
In the enum class, add the following block:
public String getName(){ return name(); }
Have the datatable declaration to look like:
<p:dataTable id="castorsDT" var="castor" value="#{managedCastorListManagedBean.initiatedCastors}" widgetVar="castorsTable" filteredValue="#{managedCastorListManagedBean.filteredCastors}">
Declare the enum-filtered column lke this:
<p:column sortBy="#{castor.castorWorkflowStatus}" filterable="true" filterBy="#{castor.castorWorkflowStatus.name}" filterMatchMode="in"> <f:facet name="filter"> <p:selectCheckboxMenu label="#{messages['status']}" onchange="PF('castorsTable').filter()"> <f:selectItems value="#{transverseManagedBean.allCastorWorkflowStatuses}" var="cws" itemLabel="#{cws.name}" itemValue="#{cws.name}"/> </p:selectCheckboxMenu> </f:facet> </p:column>
Notice how the filtering
attribute is declared:
filterable="true" filterBy="#{castor.castorWorkflowStatus.name}" filterMatchMode="in"
In other terms, the comparison is forced the rely on equals()
of class String
, through the calls to getName()
and name()
.