0

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

2
  • 1
    Formatting tip - for code blocks, select the entire region and press Ctrl-K or hit the {} button. The entire region must be indented by at least 4 spaces.
    – David Reed
    Commented Dec 20, 2018 at 22:08
  • Please note you can always repalce IF(condition, true, false) with simply condition.
    – Adrian Larson
    Commented Dec 20, 2018 at 22:28

2 Answers 2

2

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.

3
  • Thanks @David Reed. I added the other output panel with seprate condition however the second picklist is still not being rendered separately when the conditions are met (i.e., Match Partner == 'New Partner'). Any ideas?
    – FWSFDC
    Commented Dec 21, 2018 at 14:53
  • Below is the second output panel/condition: code <apex:outputPanel id="op3" > <apex:outputPanel rendered="{!PartnershipRequest__c.MatchPartner__c == 'New Partner'}" id="op4"> <apex:pageblockSection > <apex:inputField value="{!PartnershipRequest__c.MatchPartnerNew__c}"/> </apex:pageblockSection> </apex:outputPanel> </apex:outputPanel><br/>code
    – FWSFDC
    Commented Dec 21, 2018 at 14:54
  • Make sure the actionSupport rerender targets that panel too.
    – David Reed
    Commented Dec 21, 2018 at 14:55
0
<apex:actionRegion >
<apex:inputField value="{!PartnershipRequest__c.MatchPartner__c}" label="Match 
Partner">
<apex:actionSupport event="onchange" action="{!null}" rerender="op1,op3"/>  
</apex:inputField>
</apex:actionRegion>
</apex:pageblockSectionItem>

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

<apex:outputPanel id="op3" >
<apex:outputPanel rendered="{!PartnershipRequest__c.MatchPartner__c == 'New 
Partner'}" id="op4">
<apex:pageblockSection >     
<apex:inputField value="{!PartnershipRequest__c.MatchPartnerNew__c}"/>
</apex:pageblockSection>
</apex:outputPanel>
</apex:outputPanel><br/>

You must log in to answer this question.

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