Q:

Column header not visible when column is conditionally rendered

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?

A:

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>

