我正在使用jsf 2.2 Primefaces 6.0,并尝试使用primefaces的日历组件对数据表进行排序。当我开始使用特定日期过滤数据表时,所有数据表值都将消失,而不是显示正确的值。
这里有两张图片的图形说明: 没有过滤的数据表:
过滤后的数据表:
这里是xhtml代码:
<p:dataTable value="#{demandeBean.allDemandes}" var="a" id="t1"
sortMode="multiple" widgetVar="tt" editable="true" editMode="cell"
selectionMode="single" rowKey="#{a.id}"
emptyMessage="Aucune demande" rows="10" paginator="true"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="10,15,25" draggableColumns="true"
scrollable="true" scrollHeight="250" paginatorPosition="bottom">
<p:ajax event="rowSelect" listener="#{demandeBean.onRowSelect}"
oncomplete="PF('d2').show()" update=":form:d2" />
<p:ajax event="cellEdit" listener="#{demandeBean.onCellEdit}"
update=":form:t1" />
<p:column headerText="Date dépot" sortBy="#{a.dateDeDepot}" filterBy="#{a.dateDeDepot}">
<f:facet name="filter">
<p:calendar id="cal1" pattern="dd/MM/yyyy">
<p:ajax event="dateSelect" oncomplete="PF('tt').filter()" update="t1" />
<p:ajax event="change" execute="@this" oncomplete="PF('tt').filter()" update="t1"/>
</p:calendar>
</f:facet>
<h:outputText value="#{a.dateDeDepot}">
<f:convertDateTime pattern="dd/MM/yyyy HH:mm:ss"
timeZone="GMT+1:00" />
</h:outputText>
</p:column>
答案 0 :(得分:0)
添加您的专栏
filterMatchMode="contains"
这样:
<p:column headerText="Date dépot" sortBy="#{a.dateDeDepot}" filterBy="#{a.dateDeDepot}" filterMatchMode="contains">
答案 1 :(得分:0)
<p:column headerText="Date dépot" sortBy="#{a.dateDeDepot}" filterBy="#{a.dateDeDepot}" filterFunction="#{a.filterByDate}">
<f:facet name="filter">
<h:inputHidden id="filter" />
</f:facet>
<f:facet name="header">
<p:calendar id="cal1" pattern="dd/MM/yyyy">
<p:ajax event="dateSelect" oncomplete="PF('tt').filter()" update="t1" />
<p:ajax event="change" execute="@this" oncomplete="PF('tt').filter()" update="t1"/>
</p:calendar>
</f:facet>
<h:outputText value="#{a.dateDeDepot}">
<f:convertDateTime pattern="dd/MM/yyyy HH:mm:ss"
timeZone="GMT+1:00" />
</h:outputText>
</p:column>
和bean类中的函数:
public boolean filterByDate(Object value, Object filter, Locale locale) {
String filterText = (filter == null) ? null : filter.toString().trim();
if (filterText == null || filterText.isEmpty()) {
return true;
}
if (value == null) {
return false;
}
DateFormat df = SimpleDateFormat.getDateInstance(SimpleDateFormat.MEDIUM);
Date filterDate = (Date) value;
Date dateFrom;
Date dateTo;
try {
String fromPart = filterText.substring(0, filterText.indexOf("-"));
String toPart = filterText.substring(filterText.indexOf("-") + 1);
dateFrom = fromPart.isEmpty() ? null : df.parse(fromPart);
dateTo = toPart.isEmpty() ? null : df.parse(toPart);
} catch (ParseException pe) {
LOG.log(Level.SEVERE, "unable to parse date: " + filterText, pe);
return false;
} catch (Exception e) {
LOG.log("another exception");
return false;
}
return (dateFrom == null || filterDate.after(dateFrom)) && (dateTo == null || filterDate.before(dateTo));
}
}
你也可以阅读这个答案:Primefaces datatable date range filter with filterFunction