如何以编程方式应用 DataTable 过滤器? [英] How to apply DataTable filter programmatically?

查看:42
本文介绍了如何以编程方式应用 DataTable 过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 PrimeFaces Demo 过滤 DataTable (https://www.primefaces.org/showcase/ui/data/datatable/filter.xhtml) 为例,我希望能够在表格外提供过滤"链接供用户点击(比如沃尔沃、福特、宝马等).当用户单击链接时,我想在制造商过滤器下拉列表中切换所选项目并应用过滤器.我一直无法弄清楚如何进入过滤器属性进行更改.这可以通过javascript完成吗?如何访问选择列表并设置当前选择?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆