Creating Dependent SelectLists in VisualForce

C

We can create the SelectLists in VisualForce whose values will depend on the value selected in another selectlist.The method i have used here is using the Lists collection by programatically filling the values of the dependant selectlist based on the selected value.

VisualForce code:
<apex:outputLabel value=”Select State”‘>
<apex:selectList value=”{!selectedLevel1}” id=”cbxlevel1″ size=”1″ required=”true”>
<apex:selectOptions value=”{!level1items}”/>
<apex:actionSupport event=”onchange” rerender=”cbxlevel2″/>
</apex:selectList>

<apex:outputLabel value=”Select City”‘>
<apex:selectList value=”{!selectedLevel2}” id=”cbxlevel2″ size=”1″>
<apex:selectOptions value=”{!level2items}”/>
</apex:selectList>

Here in the code above, when the value of ‘State’ is selected it will populate the values in City SelectList.

APEX code:
public string selectedLevel1 {get; set;}
public string selectedLevel2 {get; set;}

public List<selectOption> level1Items {
get {
List<selectOption> options = new List<selectOption>();
options.add(new SelectOption(‘Select’,’Select’));
for(resumes__c res : [select state__c from resumes__c])
options.add(new SelectOption(res.state__c,res.state__c));
return options;
}
set;
}

Above is the code that returns the List of values to be populated in the SelectLists.

About the author

ankit.shah
By ankit.shah

Category