gpt4 book ai didi

jsf - 在 p :dataTable 上使用自定义全局过滤器

转载 作者:行者123 更新时间:2023-12-01 12:08:13 24 4
gpt4 key购买 nike

我正在尝试使用 PrimeFaces 中的全局过滤器实现逗号分隔的关键字搜索。

如果用户在全局搜索中键入word1,word2,则应返回所有包含word1word2 的行。截至目前,我无法在 PrimeFaces 中找到用于全局搜索的预定义多词搜索功能。全局搜索仅适用于单个关键字。例如:仅当用户键入 word1word2 时,搜索才会返回结果。

似乎 PrimeFaces 使用客户端 API filter() 进行全局搜索。有没有办法使用多个关键字实现搜索?

<p:dataTable id="dwg" widgetVar="tblDwgDtl" var="dwgDtl" 
value="#{dwgCtrlr.dwgs} sortMode="multiple" scrollable="true"
styleClass="bsa-drawing" rows="25" resizableColumns="true">
<f:facet name="header">
<p:panelGrid styleClass="ui-panelgrid-blank">
<p:row>
<p:column colspan="6">
<p:inputText id="globalFilter"
onkeyup="PF('tblDwgDtl').filter()"
placeholder="#{msg['searchAllFields.text']}" />
</p:column>
</p:row>
</p:panelGrid>
</f:facet>

最佳答案

PrimeFaces 8.0 及更高版本

从 PrimeFaces 8.0 开始,您可以使用 p:dataTableglobalFilterFunction 属性来实现您的自定义全局过滤器。看 https://primefaces.github.io/primefaces/8_0/#/components/datatable?id=filtering

使用示例:

<p:dataTable ... globalFilterFunction="#{dtFilterView.globalFilterFunction}">
...
</p:dataTable>
public boolean globalFilterFunction(Object value, Object filter, Locale locale) {
String filterText = (filter == null) ? null : filter.toString().trim().toLowerCase();
if (filterText == null || filterText.equals("")) {
return true;
}
int filterInt = getInteger(filterText);

Car car = (Car) value;
return car.getId().toLowerCase().contains(filterText)
|| car.getBrand().toLowerCase().contains(filterText)
|| car.getColor().toLowerCase().contains(filterText)
|| (car.isSold() ? "sold" : "sale").contains(filterText)
|| car.getYear() < filterInt
|| car.getPrice() < filterInt;
}

如果你有多个单词:

public boolean globalFilterFunction(Object rowValue, Object filter, Locale locale) {
String filterText = (filter == null) ? null : filter.toString();
if (filterText == null || filterText.isEmpty()) {
return true;
}
return Stream.of(filterText.split(","))
.allMatch(word -> singleWordFilter(value, word));
}

private boolean singleWordFilter(Object rowValue, String word) {
// do your single word filtering logic
}

PrimeFaces 8.0 之前

你可以做的是 replace the data table renderer with a custom one .然后,in there ,将 FilterFeature 替换为自定义版本。因此,您需要扩展 FilterFeaturetake care of the multiple keywords there .

关于jsf - 在 p :dataTable 上使用自定义全局过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54543368/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com