[SalesForce] Visualforce Conditionally Render 2 Different Fields based on 2 conditions

Calling VF Experts:

Is it possible to conditionally render 2 different fields on a Visualforce page based on a value selected in a picklist? I have the first condition working properly but unsure if it's possible to add another condition to render a separate field.

Below is the business scenario I am trying to solve for:

1) If Match Partner = "Existing Partner", render a field called "Match Partner Existing" OR
2) If Match Partner == "New Partner", render a separate field called "Match Partner New"

Code Snippet:

<apex:pageblockSection title="Request Details" collapsible="true" columns="1" 
id="pbs1">
   <apex:pageblockSectionItem>
   <apex:outputLabel>Match Partner</apex:outputLabel>
   <apex:actionRegion >
       <apex:inputField value="{!PartnershipRequest__c.MatchPartner__c}" 
   label="Match Partner">
       <apex:actionSupport event="onchange" action="{!null}" rerender="op1"/>  
       </apex:inputField>
   </apex:actionRegion>
   </apex:pageblockSectionItem>

    <apex:outputPanel id="op1" >
       <apex:outputPanel rendered="{!If(PartnershipRequest__c.MatchPartner__c 
   == 'Existing Partner',true,false)}" id="op2">
           <apex:pageblockSection>
            <apex:inputField value=" 
    {!PartnershipRequest__c.MatchPartnerExisting__c}"/>
           </apex:pageblockSection>
       </apex:outputPanel>
    </apex:outputPanel>
    </apex:pageblockSection>

Render

Best Answer

Sure. You've already got the pattern you need, and the conditions you have in place are entirely independent of one another - so you just need another <apex:outputPanel> with a different rendered condition.

   <apex:outputPanel rendered="{!If(PartnershipRequest__c.MatchPartner__c 
 == 'Existing Partner',true,false)}" id="op2">

Side note here: the rendered attribute is expecting a Boolean value, and PartnershipRequest__c.MatchPartner__c == 'Existing Partner' is already a Boolean expression. You can omit the IF() and just do

   <apex:outputPanel rendered="{! PartnershipRequest__c.MatchPartner__c 
 == 'Existing Partner' }" id="op2">

Your other conditional would be identical save for the text value 'New Partner', in a new <apex:outputPanel> right after (and outside) this one.

Related Topic