我正在使用 primefaces 数据表在 UI 中显示我的数据。众所周知,我们可以在数据表本身中进行排序和过滤。但是当我在数据表排序字段中键入单个字符时,它开始搜索数据,我不想要它。仅当用户在字段中键入至少 3 个字符时,我才需要搜索数据。可以这样做吗..?如果真是这样,那么是以哪种方式?请对此发表您的评论。
提前致谢。
最佳答案
我对 Primefaces 的数据表做了一个小调查,以下是我的发现。
实际上,不需要重新编译,源代码替换也可以。
您需要为过滤器事件注册一个新的处理程序,而不是 Primefaces 提供的处理程序。
在这种情况下,将像这样使用数据表:
<h:outputScript name="js/customprimefacestable.js" target="body"/>
<p:dataTable var="data" value="#{filterBean.data}" filteredValue="#{filterBean.filteredData}" widgetVar="tableWidget">
<p:column filterBy="#{data.name}" headerText="Name" filterMatchMode="contains">
<h:outputText value="#{data.name}" />
</p:column>
<p:column filterBy="#{data.value}" headerText="Value" filterMatchMode="contains">
<h:outputText value="#{data.value}" />
</p:column>
...
</p:dataTable>
JavaScript 将如下所示:
$(document).ready(function() {
tableWidget.thead.find('> tr > th.ui-filter-column > .ui-column-filter').each(function() {
var filter = $(this);
if(filter.is('input:text')) {
if(tableWidget.cfg.filterEvent!="enter"){
//unbind current handler
filter.unbind(tableWidget.cfg.filterEvent);
//bind new handler that accounts for conditional filtering
filter.bind(tableWidget.cfg.filterEvent, function(c) {
if(filter.val().length > 3) {
//Primefaces 3.5 implementation
if(tableWidget.filterTimeout){
clearTimeout(tableWidget.filterTimeout);
}
tableWidget.filterTimeout=setTimeout(function(){
tableWidget.filter();
tableWidget.filterTimeout=null},
tableWidget.cfg.filterDelay);
}
});
}
}
});
});
注意事项:
-
target="body"
: javascript 不得在<head>
中执行,因为 Primefaces 在$(document).ready()
中初始化其小部件变量,因此不能保证您的函数将在 Primefaces 初始化完成后执行; - 由于过滤从在列的搜索框中输入至少 4 个字符开始(已完成),因此当用户自行删除少于 4 个字符的文本时(未完成),您应该恢复未过滤的 View ;
- 上述解决方案针对 Primefaces 3.5
<p:dataTable>
。 Primefaces 的实现因版本而异,因此请务必检查您正在使用的版本的实现,或升级到版本 3.5; - 未涵盖使用呈现为下拉框的输入字段来过滤事件的实现;
- 该表将监听默认 (
keyup
) 事件。
关于jsf - primefaces 数据表中的排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15174924/