Primefaces datatable filter : set a filter from the xhtml page

784 Views Asked by At

I d like to create several xhtml pages using the same datatable with different filter.

For example there would be a men.xhtml and women.xhtml page from and service call getHumans; I would like the filter to be automatically applied without typing it and I dont want to create an extra view as it seems to me a lot of stuff for nothing.

I've tried these code with no success

<p:column filterBy="human.gender" filteredValue="men"
                            headerText="Gender" style="width: 150px;">

                            <h:outputText value="#{human.gender}" />
                        </p:column>

I've also tried to add this:

<p:dataTable id="gender" var="gender" filterValue="men" ...

but it didn't work

Any idea of what I can do?

1

There are 1 best solutions below

2
On

Via the data table's filterValue attribute you should provide a list to keep the filtered data if filtering is enabled. You could restrict the list you get from your bean in xhtml using expression language () with a stream:

<p:dataTable value="#{yourBean.humans.stream().filter(human -> human.getGender().equals('men')).toList()}" ...>

You need EL version 3.0 or higher for this to work.