2

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.

Dhiraj_N
  • 51
  • 1
  • 2
  • 8

2 Answers2

2

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.

Jaqen H'ghar
  • 4,305
  • 2
  • 14
  • 26
  • Thanks Jaqen for ur reply but how will it disable that column if i get true value in rendered. and is rendered attribute used only to call validation method ? ( i mean to get boolean value). – Dhiraj_N Jul 21 '14 at 13:08
  • Yes "rendered" is only used to figure out if the component should be shown (rendered). Many JSF and Primefaces components have it, they inherit it from UIComponentBase. If rendered="#{som EL expression evaluating to true}" it will be shown, and if rendered="#{some EL expression evaluating to false}" they will not be shown. See more examples how it can be used here: http://stackoverflow.com/questions/4870462/conditionally-displaying-jsf-components – Jaqen H'ghar Jul 21 '14 at 13:35
1

I think this code can help you:

<p:column rendered="#{var.login != null}">
 <h:outputText value="#{var.login}" />
</p:column>

the column will be rendered only if the var is not null

Youraf
  • 240
  • 4
  • 15