PF Filtering a Datatable column which contains a date

前端 未结 2 1773
既然无缘
既然无缘 2020-12-14 05:38

I have a column in a datatable and I want to add a filter with this code:



        
相关标签:
2条回答
  • 2020-12-14 05:58

    For simpler solution you just need to add a placeholder for a date with String as data type for filterBy component usage.

    Steps:

    Create a new transient property in the model class.

    @Transient
    private String dateForFilter;
    public String getDateForFilter() {
     return dateForFilter;
    }
    public void setDateForFilter(String dateForFilter) {
     this.dateForFilter = dateForFilter;
    }
    

    Create the logic below before returning the data model.

    public List<Item> getDataModel() {
       List<Item> lstItem = serviceClass.loadItem(userid);
       for (Item item : lstItem) {
          DateFormat dateFormat = null;
          Date date = item.getDate;
          dateFormat = new SimpleDateFormat("MM/dd/yyyy kk:mm");
          item.setDateForFilter(dateFormat.format(date));
       }
    
       return lstItem;
    }
    

    Update your XHTML to use the dateForFilter property.

    <p:column filterBy="#{item.dateForFilter}">
      <f:facet name="header">
        Transaction Date
      </f:facet>
      <h:outputText value="#{item.dateForFilter}" />
    </p:column>
    

    Note: You can only use this if you're not using the date to update the content of the model class.

    HTH.

    0 讨论(0)
  • 2020-12-14 06:05

    There is no ready-made date filter mechanism in primefaces yet, but there is a possibility to filter by date using a custom filter. You will have to define a header facet for your column and use ajax calls for "manual" filtering, but it does work:

    <column>
      <f:facet name="header">DateRange
        <div>
          <p:calendar id="from" value="#{bean.from}" styleClass="calendarFilter">
            <p:ajax event="dateSelect" listener="#{ctrlr.filterDates()}" update="dataTableId"/>
          </p:calendar>
          <p:calendar id="to" value="#{bean.to}" styleClass="calendarFilter">
            <p:ajax event="dateSelect" listener="#{ctrlr.filterDates()}" update="dataTableId"/>
          </p:calendar>
        </div>
      </f:facet>
    

    ...

    0 讨论(0)
提交回复
热议问题