jsf primefaces datatable filtering issues - jsf

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>

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.

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

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>

Related

Filter p:datatable by boolean column (checkbox)

I am trying to filter my primefaces datatable by a boolean column using a checkbox filter but unfortunately filtering in primefaces datatable seems it does not work with any type other than String, but there should be a workaround for this case.
datatable column
<p:column headerText="A_boolean_column" filterBy="#{myBean.myBoolean}" filterMatchMode="exact">
<f:facet name="filter">
<p:selectCheckboxMenu label="BooleanFilter" onchange="PF('mydatatable').filter()" styleClass="custom-filter">
<f:selectItems value="#{myBean.possibleAnswers}" />
<p:ajax update="#form" oncomplete="PF('mydatatable').filter();"/>
</p:selectCheckboxMenu>
</f:facet>
<h:outputText value="#{myBean.myBoolean}"/>
</p:column>
where possibleAnswers variable is a list that has been initialized on init method of myBean with true && false values
#PostConstruct
public void init(){
this.possibleAnswers= new ArrayList<>();
possibleAnswers.add(true);
possibleAnswers.add(false);
}
I have similar working examples in my datatable with text values and are working perfectly. Of course I could do a workaround to fix my issue by converting the values from boolean ( true / false) into String ("true" / "false") ( or even write a custom function to check for equality)but I don't really like this solution and I would prefer any other out of the box solution ( perhaps a different filterMatchMode ? ).
I am using primefaces 7.0
Normally an input component has a 'value' attribute that is bound to a field (getter/setter) in a backing bean. The type of this field can be used to automatically convert the technical string of the http request to the correct java type. For a datatable filter this cannot be automatically done since there is no value attribute. Giving all components knowledge about all possible containers they can be used in is bad design. So the only and corrrect solution is to use an explicit converter.
Have a Look at the Implementation of the Status-Column in the PrimeFaces datatable filter showcase, as far as I see it it is exactly what you need
for Reference:
<p:column filterBy="#{myBean.myBoolean}" filterMatchMode="in">
<f:facet name="filter">
<p:selectCheckboxMenu label="BooleanFilter"
onchange="PF('mydatatable').filter()" styleClass="custom-filter">
<f:converter converterId="javax.faces.Boolean" />
<f:selectItems value="#{myBean.possibleAnswers}" />
<p:ajax update="#form" oncomplete="PF('mydatatable').filter();"/>
</p:selectCheckboxMenu>
</f:facet>
</p:column>

PrimeFaces dataTable convert values for showing in a view

I have a question based in the following situation.
I have a backing bean that create a matrix with ints, in rows, columns and cells (tablaBean). I have no problem displaying it in a jsf view. Each one of those ints, it´s an ID of differents objects. How can I do, if I want to show in my JSF view, instead of ints, some attributes of the objects?
This is my dataTable:
<p:dataTable var="rowName" value="#{tablaBean.rowNames}" rowIndexVar="rowIdx">
<p:column headerText="Alumnos:">
<h:outputText value="#{rowName}"/>
</p:column>
<p:columns var="colName" value="#{tablaBean.colNames}"
headerText="#{colName}" columnIndexVar="colIdx">
<p:panel>
<h:outputText value="#{tablaBean.data2D[rowIdx][colIdx]}"/>
</p:panel>
</p:columns>
</p:dataTable>
So for example:
tablaBean.data2D[1][1]=3
But I don´t want to show the number "3" in my view.
I want to take that number and show the Person.name of my object Person.id=3;
Hope my question is clear enough.
Create in your backing bean method returning person name:
public String getPersonNameById(int idPerson) {
return myDao.findPersonById(idPerson).getName();
}
Use it in xhtml:
<h:outputText value="#{tableBean.getPersonNameById(tablaBean.data2D[rowIdx][colIdx])}"/>

Can't we give two values for one lable in JSF?

I have one label field which is license and I want give two license numbers for this..how to give two values here. I have one properties file (manageprofile) and one java class (manageprofiledatabean) which contains the license values
<h:outputLabel value="#('label.manageprofile.license')"/>
<h:outputText value="#{manageProfileDataBean.license}" />
<h:outputText escape="false" value=" "></h:outputText>
Concat both values in one string variable of manageProfileDataBean and then set it in
public class ManageProfileDataBean{
private String license = "";
private String val1 = "val1":
private String val2 = "val2";
public ManageProfileDataBean(){
license = val1+val2;
}
//setter getter
}
firstly, h:outputLabel is meant to be used for labelling input controls - it renders as the html <label> tag(e.g.)
<h:outputLabel for="myname" value="#{bean.labelValue}" />
<h:inputText id="myname" value="#{bean.name} />
If you need to specify multiple values in expression language you can do so like this:
<h:outputText value="#{bean.property1 bean.property2}"/>
so I guess in your case you would write it as
<h:outputText value="#{'label.manageprofile.license' manageProfileDataBean.license}" />
Finally, you don't need to use an h:outputText if you're not referenceing any propery so the last pair of nbsp characters don;t need to be in in outputText tag at all.

JSF datatable hide column border if column is not displayed(rendered)

I have a JSF data table
<h:dataTable id="memberTable" value="#{bean.pList}" border="0"
rowClasses="rowEven rowOdd" var="item">
<h:column rendered="#{item.isDisplay == Y}">
<h:outputText value="#{item.visitDate}" >
</h:outputText>
</h:column>
</datatable>
I have more such columns that are rendred based on a condition, when the condition is false and if I have the border=1; I see that blank cells are displayed. How can i display only the columns i want and have other ie. rendered=false not display blank cells?
Thanks,
Sai.
You can't render an entire column based on the condition of a particular row. This makes technically no sense. You need to evaluate the condition based on a property of the parent bean.
<h:dataTable value="#{bean.pList}" var="item">
<h:column rendered="#{bean.display == 'Y'}">
<h:outputText value="#{item.visitDate}" />
</h:column>
</datatable>
Move the public String getDisplay() method to the class behind #{bean}.
Note that I also fixed some other mistakes in your EL expression. You shouldn't prefix the property name with is or get and you should quote string values. You can also better make it a boolean property.
private boolean display;
public boolean isDisplay() {
return display;
}
with
rendered="#{bean.display}"

JSF Rich scroallable Datatable Issue

In a scrollable datatable, which displays a collection of objects , I have to add one manual row as first row. That row contains inputText, through which user can able to enter some values and while hitting enter, those values will save and displays in bottom rows. I couldnt able to add this manual row as first row. Below is the current code.
<rich:scrollableDataTable value="{resultList}" var="result">
<rich:column>
<f:facet name="header">Name</f:facet>
<h:outputText value="#{result.name}" />
</rich:column>
<rich:column>
<f:facet name="header">Category</f:facet>
<h:outputText value="#{result.category}" />
</rich:column>
</rich:scrollableDataTable>
The above code is for just displaying the values from the backend.
1) For that you have to use bind rich:scrollableDataTable with the instance of HtmlScrollableDataTable in the backing bean.
In backing bean, create instance of it with accessor methods & then you can initialize it accordingly by adding the inputText components. Add actionListeners to these input components & then in listeners, you can add these inputText values as outputText to the table again as rows accordingly.
2) Else you can use inputText rather then outputText & disabling the subsequent rows other then 1st, so only data may get displayed - preventing input.
<rich:scrollableDataTable value="{resultList}" var="result">
<rich:column>
<f:facet name="header">Name</f:facet>
<h:inputText value="#{result.name}" disabled ="#{!result.isFirstRow}"/>
</rich:column>
<rich:column>
<f:facet name="header">Category</f:facet>
<h:inputText value="#{result.category}" disabled ="#{!result.isFirstRow}"/>
</rich:column>
</rich:scrollableDataTable>
Backing Bean :
//---
public void initialize(){
resultList.add(new Result("", "", true)); // Setting 1st input row enabled
}
public void inputListener(ActionEvent event){
// appending object based on input to the resultList
resultList.add(new Result(inputName, inputValue, false));
// added a boolean field to identify rows added later & to make them enable/disable accordingly
}
//---
I am not familiar with Richfaces, but tried to achieve it, as I was doing it with IceFaces.

Resources