Div*_*vya 3 jsf primefaces jsf-2 primefaces-datatable
我正在尝试使用 PrimeFaces 中的全局过滤器实现逗号分隔的关键字搜索。
如果用户在word1,word2全局搜索中键入,则返回所有具有word1且word2应返回的行。截至目前,我无法在 PrimeFaces 中找到用于全局搜索的预定义多词搜索功能。全局搜索仅适用于单个关键字。例如:仅当用户键入word1或 时,搜索才会返回结果word2。
似乎 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>
Run Code Online (Sandbox Code Playgroud)
从 PrimeFaces 8.0 开始,您可以使用 的globalFilterFunction属性p:dataTable来实现您的自定义全局过滤器。见
https://primefaces.github.io/primefaces/8_0/#/components/datatable?id=filtering
使用示例:
<p:dataTable ... globalFilterFunction="#{dtFilterView.globalFilterFunction}">
...
</p:dataTable>
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
在您的多个单词的情况下:
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
}
Run Code Online (Sandbox Code Playgroud)
您可以做的是用自定义渲染器替换数据表渲染器。然后,在那里,用FilterFeature自定义版本替换。因此,您需要扩展FilterFeature并处理那里的多个关键字。
| 归档时间: |
|
| 查看次数: |
2500 次 |
| 最近记录: |