3

How can I display/hide related list(s) table on a VF page for a custom object using IF statement? I am currently displaying 3 separate related lists on my VF page using HTML markup, one of those:

<apex:outputPanel rendered="{!If(case.Parent != null, true, false)}">
   <div class="my-table">
            <h3 class="table-title">Related List: 1</h3>
            <div class="row row-header">
              <div class="column"><p>Field1</p></div>
              <div class="column"><p>Field2</p></div>
              <div class="column"><p>Field3</p></div>
              <div class="column"><p>Field4</p></div>
              <div class="column"><p>Field5</p></div>
            </div>

            <apex:repeat value="{!relCases}" var="relCase">
              <div class="row row-data" >  
                <div class="column"><p>{!relCase.field1__c}</p></div>
                <div class="column"><p>{!relCase.field2__c}</p></div>
                <div class="column"><p>{!relCase.field3__c}</p></div>
                <div class="column"><p>{!relCase.field4__c}</p></div>
                <div class="column"><p>{!relCase.field5__c}</p></div>
              </div>
            </apex:repeat>
   </div>
</apex:outputPanel>

I would like to add a IF statement in the VF page, so that, whenever the Lookup field is empty, this complete should be hidden. Any suggestions?

2
  • 2
    You can use <apex:outputPanel rendered ="{!condition}"> <!--Content to show --> </apex:outputPanel> something similar. Commented Jun 30, 2017 at 19:02
  • Could you please help me with a sample condition?
    – SatyaV
    Commented Jun 30, 2017 at 19:15

2 Answers 2

2

You can write it like this:

Just check for the list size and we are done, even no need to add the If statement here.

<apex:outputPanel rendered="{! (relCases.size > 0) }">
   <div class="my-table">
            <h3 class="table-title">Related List: 1</h3>
            <div class="row row-header">
              <div class="column"><p>Field1</p></div>
              <div class="column"><p>Field2</p></div>
              <div class="column"><p>Field3</p></div>
              <div class="column"><p>Field4</p></div>
              <div class="column"><p>Field5</p></div>
            </div>

            <apex:repeat value="{!relCases}" var="relCase">
              <div class="row row-data" >  
                <div class="column"><p>{!relCase.field1__c}</p></div>
                <div class="column"><p>{!relCase.field2__c}</p></div>
                <div class="column"><p>{!relCase.field3__c}</p></div>
                <div class="column"><p>{!relCase.field4__c}</p></div>
                <div class="column"><p>{!relCase.field5__c}</p></div>
              </div>
            </apex:repeat>
   </div>
</apex:outputPanel>
5

Wrap it in a output panel, but to be fair itzmukeshy was correct also, you don't need to do the IF

<apex:outputPanel rendered= "{!IF(yourLookup != null, true, false)}">
</apex:outputPanel>
4
  • I have used this: <apex:outputPanel rendered="{!If(case.Parent != null, true, false)}">. This is still displaying table with empty rows(row headers were displaying). Am I missing something?
    – SatyaV
    Commented Jun 30, 2017 at 19:19
  • Can you update your question with what you've done
    – EricSSH
    Commented Jun 30, 2017 at 19:29
  • Updated the code.
    – SatyaV
    Commented Jun 30, 2017 at 19:30
  • 1
    It looks like you only want to show the related cases List, if that case has a Parent so I would actually check to see if the relCases.isEmpty (null or size) rather than case.Parent. I don't see anything wrong with what you did but you should put some System.debug(); in your controller to see what may be happening
    – EricSSH
    Commented Jun 30, 2017 at 19:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .