PF Filtering a Datatable Column That Contains a Date

I have a column in a datatable and I want to add a filter using 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> 

the date is entered in the form in this format:

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

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

+6
source share
2 answers

While there is no ready-made mechanism for filtering dates in streams, it is possible to filter by date using a custom filter. You will need to define a header facet for your column and use ajax calls for manual filtering, but it works:

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

...

+11
source

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

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 use this only if you are not using a date to update the contents of the model class.

NTN.

+7
source

Source: https://habr.com/ru/post/917155/


All Articles