SteveS
SteveS

Reputation: 1030

How to apply DataTable filter programmatically?

Using the PrimeFaces Demo filtering DataTable (https://www.primefaces.org/showcase/ui/data/datatable/filter.xhtml) as an example, I would like to be able to provide "filtering" links outside of the table for the user to click (say Volvo, Forw, BMW, etc). When the user clicks the link, I would like to switch the selected item in the manufacturer filter dropdown and apply the filter. I haven't been able to figure out how to get to the filter properties to make the change. Can this be done via javascript? How do I access the selection list and set the current selection?

UPDATE:
Following Daniel's link, I managed to get the dropdown selection to change, but I am unable to get the filter to apply. In the columns where the filters are based on an input field, triggering the keyup causes the data to filter but I can't figure out what event to trigger on the select to make it filter. Here is the code I am using:

<p:commandLink id="filterLink" 
    value="Click to filter to Volvo Only" 
    onclick="$('#carForm\\:dataTable\\:manufacturerColumn_filter').val('Volvo'),
        $('#carForm\\:dataTable\\:manufacturerColumn_filter').trigger('filter')"
/>

The link created by the above will switch the Manufacturer filter to Volvo but will not cause the data to filter.

Upvotes: 2

Views: 12144

Answers (1)

SteveS
SteveS

Reputation: 1030

The key is to call the filter event at the table level. So my code above should properly be:

<p:commandLink id="filterLink" 
value="Click to filter to Volvo Only" 
onclick="$('#carForm\\:dataTable\\:manufacturerColumn_filter').val('Volvo'), carsTable.filter()"
/>

Upvotes: 1

Related Questions