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
lovetolearnlovetolearn 

Displaying Conditional Fields

Hi,

 

I am trying to construct a page where the fields displayed is dependent on the value selected from the picklist. Below is as far as I got:

<apex:page controller="statusController">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:selectList size="1" id="Status" label="Status" multiselect="false" value="{!Status}">
                    <apex:selectOption itemLabel="Before" itemValue="Before"/>
                    <apex:selectOption itemLabel="During" itemValue="During"/> 
                    <apex:selectOption itemLabel="After" itemValue="After"/>                 
                </apex:selectList>
                <apex:actionSupport event="onchange" reRender="statusField"/>
            </apex:pageBlockSection>
                <apex:outputPanel id="statusField">
                    
                </apex:outputPanel> 
        </apex:pageBlock>    
    </apex:form>
</apex:page>


public class statusController {

    public String Status {get; set;}

}

 Not sure where to specify the conditions (If statments) and how to specify what fields to display. Please help. Thank you.

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

You can use the rendered attribute of an inputField (or other visual component). You can furthermore use traditional formula statements. Here's an example:

 

<apex:inputField value="{!object__c.field__c}" rendered="{!status='Before'}"/>
<apex:inputText value="{!textvariable}" rendered="{!status='During'}"/>
<apex:selectList size="1" value="{!selectedValue}" disabled="{!status='Before'}" rendered="{!not Status='During'}">
    <apex:selectOptions value="{!selectOptions}" />
</apex:selectList>

You can use virtually any operator listed in Help & Training under formulas, so long as the result is a boolean value, including ISNULL, ISBLANK, CASE, OR, AND, NOT, equality and inequality operators (lesser, greater, equals, not equal, lesser than, greater than). Also of note, you can use special syntaxes that apply only to Visualforce. You should read the Visualforce Developer's Guide for more information there as well.

All Answers

sfdcfoxsfdcfox

You can use the rendered attribute of an inputField (or other visual component). You can furthermore use traditional formula statements. Here's an example:

 

<apex:inputField value="{!object__c.field__c}" rendered="{!status='Before'}"/>
<apex:inputText value="{!textvariable}" rendered="{!status='During'}"/>
<apex:selectList size="1" value="{!selectedValue}" disabled="{!status='Before'}" rendered="{!not Status='During'}">
    <apex:selectOptions value="{!selectOptions}" />
</apex:selectList>

You can use virtually any operator listed in Help & Training under formulas, so long as the result is a boolean value, including ISNULL, ISBLANK, CASE, OR, AND, NOT, equality and inequality operators (lesser, greater, equals, not equal, lesser than, greater than). Also of note, you can use special syntaxes that apply only to Visualforce. You should read the Visualforce Developer's Guide for more information there as well.

This was selected as the best answer
JBabuJBabu

You need to add the output text tag and display the output and also your mistake is you need to end selectList outside the action support.

Here you go with the code:  (and the controller class remains same)

 

<apex:page controller="statusController">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:selectList size="1" id="Status" label="Status" multiselect="false" value="{!Status}">
<apex:selectOption itemLabel="Before" itemValue="BEFORE"/>
<apex:selectOption itemLabel="During" itemValue="DURING"/>
<apex:selectOption itemLabel="After" itemValue="AFTER"/>
<apex:actionSupport event="onchange" reRender="statusField"/>
</apex:selectList>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:outputPanel id="statusField">
<p> You have selected </p>
<apex:outputText value="{!Status}"></apex:outputText>
</apex:outputPanel>
</apex:form>
</apex:page>

 

 

Thanks,

JBabu.

 

lovetolearnlovetolearn

Thank you both. They both worked. The first reply was closer to what I was looking for, but thank you.