How to apply DataTable filter programatically?

匿名 (未验证) 提交于 2019-12-03 09:13:36

问题:

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 javescript? 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.

回答1:

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()" /> 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!