Primefaces中DataTable过滤器和日历选择器的冲突

The conflict between the DataTable filter and the calendar picker in Primefaces

我正在使用 JavaEE 7、Primefaces 5.2 和 GlassFish 4.1 build 13。

我在同时使用 DataTable 筛选器和日历选择器时遇到问题。在该页面中,有一个数据 table,其中每列都有过滤器。单独的日历选择器用于更新数据 table(将显示所选日期之后的任何内容)。如果没有使用列过滤器,日历选择器工作正常。但是,如果使用其中一个列过滤器,日历选择器将停止更新数据 table。无论是在日志中还是通过调试都没有发现错误。

页面中相关代码为:

    <h:form>
        <p:outputLabel styleClass="header-calendar">From Date: </p:outputLabel>
        <p:calendar id="fromDate" value="#{datedCarFilterView.fromDate}" pattern="dd MM yyyy"  readonlyInput="true" maxdate="#{datedCarFilterView.currentDate}">
            <p:ajax event="dateSelect" update="viewDataTable" />
        </p:calendar><br/>   
        <p:dataTable var="car" value="#{datedCarFilterView.cars}" id="viewDataTable">

            <p:column headerText="ID">
                <h:outputText value="#{car.id}" />
            </p:column>
            <p:column filterBy="#{car.color}" headerText="Color" filterMatchMode="contains">
                <h:outputText value="#{car.color}" />
            </p:column>    
            <p:column headerText="Date">
                <h:outputText value="#{car.date}">
                    <f:convertDateTime pattern="dd-MMM-yyyy" />
                </h:outputText>
            </p:column>                  
        </p:dataTable>
    </h:form>

相关Java代码如下:

@ManagedBean @ViewScoped public class DatedCarFilterView implements Serializable {

private static final long serialVersionUID = 978770613134439198L;

private Date fromDate;

private List<DatedCar> allCars;
private final List<DatedCar> cars = new ArrayList<>();

@ManagedProperty("#{datedCarService}")
private DatedCarService service;

@PostConstruct
public void init() {
    allCars = service.createCars(100);
    fromDate = new Date(getCurrentDate().getTime() - 1000 * 3600 * 24 * 3);
    refreshCarList(fromDate);
}

public void setService(DatedCarService service) {
    this.service = service;
}

public List<DatedCar> getCars() {
    return cars;
}

public Date getFromDate() {
    return fromDate;
}

public void setFromDate(Date fromDate) {
    this.fromDate = fromDate;
    refreshCarList(fromDate);
}

private void refreshCarList(Date fromDate1) {
    cars.clear();
    for (DatedCar car : this.allCars) {
        if (car.getDate().getTime() > fromDate1.getTime()) {
            cars.add(car);
        }
    }
}

public Date getCurrentDate() {
    return new Date();
} }

如有任何帮助,我们将不胜感激。

谢谢。

原来滤镜在使用后还处于使用状态。在正确执行另一个相关的外部操作之前,需要调用清除代码。以下代码更改将使其工作:

<p:ajax event="dateSelect" update="viewDataTable" onstart="PF('vtWidget').clearFilters()"/>

<p:dataTable var="car" value="#{datedCarFilterView.cars}" id="viewDataTable" widgetVar="vtWidget">