+ Start a Discussion
SivaGSivaG 

Custom Picklist default value based on other Picklist value in Visualforce page

Hi,

I have two picklist values as shown below

1st picklist 
<apex:selectOption itemValue="None" itemLabel="--None--" />
<apex:selectOption itemValue="No" itemLabel="No" />
<apex:selectOption itemValue="Yes" itemLabel="Yes"/>

2nd Picklist has values coming from 
None
A
B

Requirements  -

1) I see 'No' as the default value in the 1st picklist. I wanted None to be the default.

2) If 1st picklist value 'Yes' is selected then 2nd Picklist value B should be selected by default. How can we achieve this?

Thanks
Kumar

 
Best Answer chosen by SivaG
Sampath KumarSampath Kumar

Hi Kumar,

Please find the visualforce page and apex class which solves your requirement.

Visualforce page
===========
<apex:page controller="SelectValues">
<apex:form > 
    <apex:outputlabel value="Pick List 1"/> 
        <apex:actionRegion >      
        <apex:selectList value="{!selectPickList1}" size="1">
        <apex:selectOptions value="{!PickList1}"/>
        <apex:actionSupport event="onchange" rerender="myFields"/>
        </apex:selectList>
        </apex:actionRegion>                         
    <apex:outputlabel value="Pick List 2 :"/>   
    <apex:outputPanel id="myFields">   
    <apex:actionRegion >  
    <apex:selectList value="{!selectPickList2}" size="1">
    <apex:selectOptions value="{!PickList2}"/>
    </apex:selectList>
    </apex:actionRegion>      
    </apex:outputPanel>
</apex:form>
</apex:page>

Apex Class:
========
public class SelectValues
{
        public String selectPickList1{get; set;}
        public String selectPickList2{get; set;}
        Public SelectValues()
        {   
            selectPickList1= '--None--';
            selectPickList2 = 'None';
        }
        public List<SelectOption> getPickList1() 
        {
            List<SelectOption> pick1= new List<SelectOption>();
            pick1.add(new SelectOption('--None--','--None--'));
            pick1.add(new SelectOption('Yes','Yes'));
            pick1.add(new SelectOption('No','No'));
            return pick1;
        }
        public List<SelectOption> getPickList2() 
        { 
            List<SelectOption> pick2= new List<SelectOption>();
            pick2.add(new SelectOption('--None--','--None--'));
            pick2.add(new SelectOption('A','A'));
            pick2.add(new SelectOption('B','B'));
            if(selectPickList1 == 'Yes')
            {
                    selectPickList2  = 'A';
            }
            else if(selectPickList1 == 'No'){
                    selectPickList2  = 'B';
            }
            return pick2;
        }      
}

Regards
Sampath Kumar Goud