【发布时间】:2016-07-21 16:41:48
【问题描述】:
我做了一个 PrimeFaces 数据表,可以过滤(见下文)。它工作正常,但现在我尝试创建按开始和结束日期过滤的功能,通过日历选择日期(它应该找到这两个日期之间的日期;注意顶部的开始日期/结束日期和过滤日期按钮) .我需要它按开始和结束日期过滤,就像它按其他所有内容过滤一样。
它是通过自定义方法调用的,但它不会返回正确的值,最重要的是,如果我在分页器上选择另一个页面,它会默认返回未过滤列表。这是一场灾难。我需要帮助,以便用户可以按开始/结束日期过滤作为整个过滤系统的一部分。现在开始/结束日期字段是标题的一部分。
我想如果过滤日期字段修改了用于为表 (tableFiltered) 拉取数据的列表(bean 属性),这将确保拉取的数据始终限于已过滤的正确日期。但它没有那样工作。我不明白。
在 home.xhtml 中
<p:outputPanel id="tableContainer">
<p:remoteCommand name="filterByDate" action="#{homeController.FilterByDate()}" update="tableContainer" />
<p:dataTable var="hbel" id="hbelList" value="#{homeController.tableFiltered}" rows="10"
paginator="true"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="10,100,1000"
widgetVar="widgetTable"
tableStyle="width:auto">
<f:facet name="header">
<p:outputPanel>
<p:calendar id="calStartDate" value="#{homeController.startDate}" maxdate="#{homeController.endDate}" navigator="true" pattern="dd-MMM-yy" placeholder=" start date">
<p:ajax process="calStartDate" partialSubmit="true" event="change"/>
</p:calendar>
<p:calendar id="calEndDate" value="#{homeController.endDate}" maxdate="#{homeController.endDate}" navigator="true" pattern="dd-MMM-yy" placeholder=" end date">
<p:ajax process="calEndDate" partialSubmit="true" event="change"/>
</p:calendar>
<p:commandButton id="btnFilter" value="Filter Dates" ajax="true">
<p:ajax oncomplete="filterByDate()" />
</p:commandButton>
</p:outputPanel>
</f:facet>
<p:column filterStyle="width: 48px;" filterBy="#{hbel.batchId}" headerText="BatchID" filterMatchMode="exact">
<h:outputText value="#{hbel.batchId}" />
</p:column>
<p:column filterStyle="width: 48px;" filterBy="#{hbel.recordId}" headerText="RecordID" filterMatchMode="exact">
<h:outputText value="#{hbel.recordId}" />
</p:column>
<p:column filterBy="#{hbel.unitName}" headerText="UnitName" filterMatchMode="in">
<f:facet name="filter">
<p:selectCheckboxMenu label="Unit" onchange="PF('widgetTable').filter()" panelStyle="width:125px" scrollHeight="150">
<f:selectItem itemLabel="Location" itemValue="LOCATION"/>
<f:selectItem itemLabel="Unit" itemValue="UNIT" />
<f:selectItem itemLabel="Customer" itemValue="DIRECT_CUSTOMER" />
<f:selectItem itemLabel="Occupancy" itemValue="OCCUPANCY" />
<f:selectItem itemLabel="Bank Account" itemValue="BANK_ACCOUNT" />
</p:selectCheckboxMenu>
</f:facet>
<h:outputText value="#{hbel.unitName}" />
</p:column>
<p:column filterBy="#{hbel.logDate}" headerText="LogDate" filterMatchMode="exact">
<h:outputText value="#{hbel.logDate}" />
</p:column>
<p:column filterBy="#{hbel.logFlag}" headerText="LogFlag" filterMatchMode="in">
<f:facet name="filter">
<p:selectCheckboxMenu label="Flag" onchange="PF('widgetTable').filter()" panelStyle="width:125px" scrollHeight="150">
<f:selectItem itemLabel="Business" itemValue="B"/>
<f:selectItem itemLabel="Reconciliation" itemValue="R" />
<f:selectItem itemLabel="Technical" itemValue="T" />
<f:selectItem itemLabel="Warning" itemValue="W" />
</p:selectCheckboxMenu>
</f:facet>
<h:outputText value="#{hbel.logFlag}" />
</p:column>
<p:column filterBy="#{hbel.logFields}" headerText="LogFields" filterMatchMode="contains">
<h:outputText value="#{hbel.logFields}" />
</p:column>
<p:column filterBy="#{hbel.logReason}" headerText="LogReason" filterMatchMode="contains">
<h:outputText value="#{hbel.logReason}" />
</p:column>
</p:dataTable>
</p:outputPanel>
在 homeController.java bean 内部(请求范围)
// custom filtering
public void FilterByDate()
{
if (startDate != null &&
endDate != null)
{
// new dates (add start and end date to range)
Calendar c = Calendar.getInstance();
c.setTime(startDate);
c.add(Calendar.DATE, -1);
Date startDateMod = c.getTime();
c.setTime(endDate);
c.add(Calendar.DATE, 1);
Date endDateMod = c.getTime();
// reset table first
tableFiltered.clear();
// add legal items from tableFull
for (HubToBauExceptionLog hbel : tableFull)
{
if (hbel != null &&
hbel.getLogDate().after(startDateMod) &&
hbel.getLogDate().before(endDateMod))
{
tableFiltered.add(hbel);
}
}
}
}
【问题讨论】:
标签: date jsf primefaces pagination