I have created one dataTable in page and shown some columns but I want to show only those columns which have entries ( if any column doesn't have record then it should not be displayed on the page.) . Anyone please tell me how should I put validation on this and display table.
2
votes
2 Answers
2
votes
You can use the rendered attribute on the column.
For example if your current code is like in the Primefaces showcase (where the entity is Car) you create a method on the bean for every column you would want to omit if all rows are empty. For example for the column "Color":
public Boolean hasAnyCarColor() {
for (Car car : cars) {
if (!(car.getColor() == null) && !(car.getColor().isEmpty()))
return true;
}
return false;
}
In the view do:
<p:column rendered="#{testBean.hasAnyCarColor()}">
....
</p:column>
If there are many rows you would probably want to cache the Boolean's in some attributes on the bean.