过滤p:dataTable按日期范围和分页

时间:2016-07-21 16:41:48

标签: date jsf primefaces pagination

我制作了PrimeFaces数据表,可以对其进行过滤(见下文)。它工作正常,但现在我试图创建按开始和结束日期过滤,通过日历选择日期的功能(它应该找到这两个日期之间的日期;通知开始日期/结束日期和过滤日期按钮在顶部) 。我需要它按开始和结束日期进行过滤,就像过滤其他所有内容一样。

它是通过自定义方法调用的,但它不会返回正确的值,最重要的是,如果我在分页器上选择另一个页面,它会默认返回到未过滤的列表。这是一场灾难。我想帮助它,以便用户可以按开始/结束日期过滤作为整个过滤系统的一部分。现在,开始/结束日期字段是标题的一部分。

我想也许如果过滤日期字段修改用于拉取表的数据的列表(bean属性)(tableFiltered),这将确保拉出的数据始终限于已过滤的正确日期。但它不是那样的。我不明白。

enter image description here

Inside 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);
                }
            }
        }
}

1 个答案:

答案 0 :(得分:1)

来自Primefaces docs:

  

<强>过滤

     

通过设置filterBy at来启用基于Ajax的过滤   列级别并提供列表以保留已过滤的子列表。它是   建议使用比请求镜像更长的范围来保持   filteredValue使得过滤后的列表仍然可以访问   过滤

<p:dataTable var="car" value="#{carBean.cars}"
             filteredValue="#{carBean.filteredCars}">
    <p:column filterBy="#{car.model}" headerText="Model">
        <h:outputText value="#{car.model}" />
    </p:column>
    ...more columns
</p:dataTable>

因此,尝试向dataTable添加filteredValue的列表,将tableFull保留为值,并在FilterByDate方法中填充已过滤的列表。

<p:dataTable var="hbel" id="hbelList" 
    value="#{homeController.tableFull}" rows="10"
    filteredValue="#{homeController.tableFiltered}"
    paginator="true"
    paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
    rowsPerPageTemplate="10,100,1000"
    widgetVar="widgetTable"
    tableStyle="width:auto">
相关问题