如何以编程方式应用 DataTable 过滤器? [英] How to apply DataTable filter programmatically?
问题描述
使用 PrimeFaces Demo 过滤 DataTable (
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?
更新:
按照丹尼尔的链接,我设法改变了下拉选择,但我无法应用过滤器.在过滤器基于输入字段的列中,触发 keyup 会导致数据过滤,但我无法弄清楚在选择上触发什么事件以使其过滤.这是我正在使用的代码:
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.
推荐答案
关键是在表层调用过滤事件.所以我上面的代码应该是:
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()"
/>
这篇关于如何以编程方式应用 DataTable 过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!