ads

Tuesday, 16 August 2016

Dependent Picklist In Visualforce Page


Controller Code : -


Public Class DependentPickListController {

    Public String Country{get;set;}
    Public String State{get;set;}

    Public List <SelectOption> getCountries() {
        List <SelectOption> Options = New List <SelectOption> ();
        Options.add(new SelectOption('None','None'));
        Options.add(new SelectOption('IN','India'));
        Options.add(new SelectOption('US','United States'));
        Options.add(new SelectOption('UK','United Kingdom'));
        Return Options;
    }
    Public List < SelectOption > getStates() {
        List < SelectOption > Options = New List< SelectOption > ();
        if (Country == 'IN') {

            Options.add(new SelectOption('DL','Delhi'));
            Options.add(new SelectOption('MP','Madya Pradesh'));
            Options.add(new SelectOption('JK','Jammu Kashmir'));
            
        } else if (Country == 'US') {

            Options.add(new SelectOption('AL', 'Alabama'));
            Options.add(new SelectOption('ALK', 'Alaska'));
            Options.add(new SelectOption('CL', 'California'));
            
        } else if (Country == 'UK') {

            Options.add(new SelectOption('BR', 'Bristol'));
            Options.add(new SelectOption('CM', 'Cambridge'));
            Options.add(new SelectOption('CH', 'Chester'));
            
        } else {
            Options.add(new SelectOption('None', 'None'));
        }
        Return Options;
    }

}


Visualforce Page Code :-


<apex:page controller="DependentPickListController">
    <apex:form >
        <apex:pageblock >
            <apex:pageblockSection columns="2">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Country" />
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:selectList size="1" value="{!Country}">
                        <apex:selectOptions value="{!Countries}" />
                        <apex:actionSupport event="onchange" rerender="id1" />
                    </apex:selectList>
                </apex:pageBlockSectionItem>
                <apex:pageblockSectionItem >
                    <apex:outputLabel value="State " />
                </apex:pageblockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:selectList size="1" value="{!State }" id="id1" >
                        <apex:selectOptions value="{!States}" />
                       
                    </apex:selectList>
                </apex:pageBlockSectionItem>
            </apex:pageblockSection>
        </apex:pageblock>
    </apex:form>
</apex:page>


Output Screen 1 :-












No comments:

Post a Comment