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
lavanya gottumukkalalavanya gottumukkala 

Dynamically displaying picklist values based on value selected in another picklist field in a vf page

Hi All,

We are using a visualforce page and a field 'Taxtype'(Input picklist field contains 10 values) in that page.We are displaying that field from fieldset.Now we got the below requirement.

Based on the country that user selects while registration,we need to display only the related values dynamically in 'Taxtype' field,not all 10 values when he/she redirected to that particular page.

How can we achieve this requirement through apex and visualforce?

Thanks in advance.
jyothsna reddy 5jyothsna reddy 5
Hi Lavanya

Please try the below sample code:
Visual force page:
<apex:page controller="sample">
    
    <apex:form >
    
    <apex:pageBlock>
        <apex:pageBlockSection columns="2">
            <apex:pageblockSectionItem>
                <apex:outputLabel value="State"/>
            </apex:pageblockSectionItem>        
            <apex:pageblockSectionItem>                
                <apex:selectList size="1" value="{!state}">
                    <apex:selectOptions value="{!states}"/>
                    <apex:actionSupport event="onchange" reRender="a"/>
                </apex:selectList>                
            </apex:pageblockSectionItem>
            <apex:pageblockSectionItem>
                <apex:outputLabel value="City"/>
            </apex:pageblockSectionItem>            
            <apex:pageblockSectionItem>
                <apex:selectList size="1" value="{!city}" id="a">
                    <apex:selectOptions value="{!cities}"/>
                </apex:selectList>
            </apex:pageblockSectionItem>            
        </apex:pageBlockSection>        
    </apex:pageBlock>

    </apex:form>

</apex:page>
Controller:
 
public class sample
{
    public String state {get;set;}
    public String city {get;set;}

    public List<SelectOption> getStates()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('None','--- None ---'));        
        options.add(new SelectOption('TN','Tamil Nadu'));
        options.add(new SelectOption('KL','Kerala'));
        return options;
    } 
    
    public List<SelectOption> getCities()
    {
        List<SelectOption> options = new List<SelectOption>();
        if(state == 'TN')
        {       
            options.add(new SelectOption('CHE','Chennai'));
            options.add(new SelectOption('CBE','Coimbatore'));
        }
        else if(state == 'KL')
        {       
            options.add(new SelectOption('COA','Coachin'));
            options.add(new SelectOption('MVL','Mavelikara'));
        }
        else
        {
            options.add(new SelectOption('None','--- None ---'));
        }      
        return options;
    }       
}

I hope It will help you.

Regards,
Jyothsna D
 
Allen ManamelAllen Manamel
This worked like a charm but I was wondering is there a way we could pass the state value as a parameter and based on that displays the city instead of writing if(state == 'TN')  else if(state == 'KL') as we can have n number of states. Also how can we save the state and city selected by the user on the backend?