【问题标题】:The conflict between the DataTable filter and the calendar picker in PrimefacesPrimefaces中DataTable过滤器和日历选择器的冲突
【发布时间】:2016-01-07 10:06:01
【问题描述】:

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

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

页面中相关代码为:

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

任何帮助将不胜感激。

谢谢。

【问题讨论】:

    标签: jsf jsf-2 primefaces calendar


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-04
      • 1970-01-01
      • 2011-03-08
      • 2014-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多