3

I have a h:dataTable. I would like to render a row conditionally as follows:

<h:dataTable value="#{myController.entities}" var="entity">
 <h:column rendered="#{entity.selected}">
  <f:facet name="header">
   <h:outputText value="header" />
  </f:facet>
  <h:outputText value="#{entity.name}" />
 </h:column>
</h:dataTable>

In my controller there is variable List <Entity> entities which has getters and setters.

Entity class contains two variables : boolean selected and String name with its getters and setters.

When #{entity.selected} is false, then the row is hidden. When #{entity.selected} is true, then the row is shown, but without header. When I do not use the rendered attribute on the column my header becomes visible.

This question is similar to p:column header facet is not shown when rendered attribute is used on p:column

But none of the solution given works for me and Iam not using primefaces.

Can somebody help me with this? Iam new to JSF and the mistake might be something silly and fix fairly easy, but please help me figure it out. Thanks.

Iam posting the outputs that I get when I tried the above code.

**Header**

name 0

name 1

name 2

name 3

name 4

This output shows the datatable using simply <h:column> without trying to render it conditionally. The header can be seen.

Now when I use conditional rendering:

name 1

name 3

In this output Iam trying to use conditional rendering <h:column rendered="#{entity.selected}">. As you can see that for "name 0", "name 3" and "name 5" the #{entity.selected} is false and hence its not rendering. This is exactly what I need but only if the header shows up.

I want to render my row invisible where the #{entity.selected} value is false. Also show the column header. Can this be possible?

yassadi
  • 524
  • 1
  • 9
  • 20
  • 2
    Why are you trying to render a **whole** column conditionally based on a property of a *single* row? How about other rows? How does this technically make sense? Shouldn't you instead the condition in `#{myController}` instead? – BalusC Aug 26 '14 at 14:03
  • Related/dupe: http://stackoverflow.com/questions/11208721/jsf-datatable-hide-column-border-if-column-is-not-displayedrendered/11208832#11208832 http://stackoverflow.com/questions/16982720/pcolumn-rendered-attribute-does-not-seem-to-work-with-logical-operators/16982840#16982840 – BalusC Aug 26 '14 at 14:03
  • Thank a lot BaluC for responding. You are correct of course. Its not the column I want not to render its the entire row. Being fairly new to JSF and datatables and also my datatable having just one column, got me confused. I have edited the question accordingly. – Lineesh Antony Aug 27 '14 at 06:36

2 Answers2

4

You need to put the rendered condition on the cell, not on the column.

<h:dataTable value="#{myController.entities}" var="entity">
 <h:column>
  <f:facet name="header">
   <h:outputText value="header" />
  </f:facet>
  <h:outputText value="#{entity.name}" rendered="#{entity.selected}" />
 </h:column>
</h:dataTable>

Or, if you've multiple components or plain text representing cell content, wrap it in a <h:panelGroup> or <ui:fragment>.

<h:dataTable value="#{myController.entities}" var="entity">
 <h:column>
  <f:facet name="header">
   header
  </f:facet>
  <h:panelGroup rendered="#{entity.selected}">
   Blah blah #{entity.name} blah blah
  </h:panelGroup>
 </h:column>
</h:dataTable>

(note: you don't necessarily need h:outputText over all place, see also Is it suggested to use h:outputText for everything?)

Apply if necessary the following CSS on the table to hide empty cells in case you've applied e.g. borders or bgcolors on table cells:

table {
    empty-cells: hide;
}

A completely different alternative is to provide a new model which contains only the desired entities:

<h:dataTable value="#{myController.selectedEntities}" var="entity">
 <h:column>
  <f:facet name="header">
   header
  </f:facet>
  #{entity.name}
 </h:column>
</h:dataTable>
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Perfect! The css part did the trick. Thanks a lot BaluC!! I would have liked to give +1 as well, not enough reputation though :) – Lineesh Antony Aug 27 '14 at 09:48
-2

Instead of

...
    <f:facet name="header">
       <h:outputText value="header" />
      </f:facet>
...

try simply

...
    <f:facet name="header">Header</f:facet>
...
Jas
  • 1,141
  • 5
  • 16