Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsf primefaces datatable filtering issues

Tags:

jsf

primefaces

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> 
like image 720
Sven Avatar asked Jan 25 '11 13:01

Sven


2 Answers

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.

like image 98
Nino Estole Avatar answered Nov 12 '22 19:11

Nino Estole


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

like image 2
Andre Avatar answered Nov 12 '22 19:11

Andre