Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PF Filtering a Datatable column which contains a date

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

<p:dataTable id="aDataTable" var="aVariable" value="#{aView.values}" paginator="true" rows="10" selectionMode="single" selection="#{aView.selection}" onRowSelectUpdate="aForm">
                 <f:facet name="header">
                     A List
                 </f:facet>
              <p:column sortBy="#{aVariable.id}" filterBy="#{aVariable.id}" filterEvent="change">
                  <f:facet name="header">
                      <h:outputText value="No"/>
                  </f:facet>
                   <h:outputText value="#{aVariable.id}"/>
              </p:column>
              <p:column sortBy="#{aVariable.date}" filterBy="#{aVariable.date}" filterEvent="change">
                  <f:facet name="header">
                      <h:outputText value="Date"/>
                  </f:facet>
</p:dataTable>

date is inputted in a form in this format:

<h:outputText value="Date: *"/>
<p:calendar pattern="dd/MM/yyyy" value="#{aView.value.date}"/>

Filter is working for id but not for date. What is the reason for this and how can I make filter work in this case?

like image 679
lamostreta Avatar asked Jun 01 '12 07:06

lamostreta


2 Answers

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>

...

like image 171
kostja Avatar answered Nov 15 '22 23:11

kostja


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.

like image 7
Nino Estole Avatar answered Nov 15 '22 23:11

Nino Estole