function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jeffrey CheungJeffrey Cheung 

How to hide empty column in apex:table?

<apex:dataTable value="{!Contacts}" var="c"> 
  <apex:column rendered="if all contacts has number"> 
    <apex:outputText>{!c.number}</apex:outputText> 
  </apex:column> 
</apex:dataTable>


For example, hide the number column if all contacts has no number.
 
Suraj TripathiSuraj Tripathi

Hi Jeffrey Cheung,

Please try this piece of code for your validation

<apex:dataTable value="{!Contacts}" var="c"> 
  <apex:column rendered="{!c.number!=null}"> 
    <apex:outputText>{!c.number}</apex:outputText> 
  </apex:column> 
</apex:dataTable>

Add rendered into your code

rendered="{!c.number!=null}"


Hope it will help yo,

Regards,

Suraj

Jeffrey CheungJeffrey Cheung
Sorry @suraj this is not working. Your code will make column disappear if any c.number is null, but I want column disappear if ALL c.number is null.
Sukesh Kumar SFSukesh Kumar SF
In this case you will have to use a boolean variable from Controller.

It would be set to true if all number field is null.

Just as an example:

Controller:
for(Contact objContact : lstContact) {
  if(objContact.number !=null)
    isColumnDisplay = true;
}
Page:
<apex:column rendered="{!isCoulmnDisplay}">
    <apex:outputText>{!c.number}</apex:outputText>
  </apex:column>

Hope this helps!
Thanks
 
Waqar Hussain SFWaqar Hussain SF
Hi Jeffrey, 

Please try the below code snippet and let me know the result.
 
<apex:dataTable value="{!Contacts}" var="c">

	<apex:column>
		<apex:facet name="header">Number</apex:facet>
		<apex:outputText  rendered="{!NOT(ISNULL(c.number))}" value="{!c.number}"/>
	</apex:column>

</apex:dataTable>

Thanks