7
votes

I am using primefaces and its datatable. A few columns are dates or currencies. If I try to filter those, there are awkward behaviours. When I start typing the filter works until the first delimiter (dot for date for example, so it only filters for 11. the next character let the table to display no entry).

Is it possible to apply a dateconverter?

Here is my code for now:

<p:column filterBy="#{cou.startDate}"
    headerText="#{text['date']}"
    filterMatchMode="contains" 
    sortBy="#{cou.startDate}" >
        <h:outputText value="#{cou.startDate}" >
             <f:convertDateTime pattern="dd.MM.yyyy" />
        </h:outputText>
</p:column> 
3

3 Answers

3
votes

Instead of directly using the cou.startDate from the model, you can instead do the following:

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.

2
votes

As far as I know, you can't use a converter for the filter value. You can, however, deal with that in your bean/service/dao logic.

You could hardcode your logic and use SimpleDateFormat to parse the value if the filter column matches certain name like startDate or endDate. A more generic approach would be to use reflection to get the Class associated with the column and use SimpleDateFormat if it's Date, DecimalFormat if it's a number and so on.

Naturally if you're propagating that query to a database, you won't be able to use the like operator. If you're using a number you'll need to compare for equality (same applies to dates). If you're looking for stuff that's in memory you'll have to change you logic a little bit. But it shouldn't be too bad. If you could post some of your backing bean/service code, I guess I could be a little more helpful ;)

2
votes

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:

<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>