0

I select the country in the frist field than should show the second field the state, that works fine. But If I select the state I want to select the city in the third field. It doesn't work.

<apex:page controller="ctlDepPickLst">
    <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="a"/>
                </apex:selectList>                
            </apex:pageblockSectionItem>
            <apex:pageblockSectionItem>
                <apex:outputLabel value="State"/>
            </apex:pageblockSectionItem>            
            <apex:pageblockSectionItem>
                <apex:selectList size="1" value="{!state}" id="a">
                    <apex:selectOptions value="{!states}"/>
                    <apex:actionsupport event="onchange" reRender="b"/>
                </apex:selectList>
            </apex:pageblockSectionItem>  
            <apex:pageblockSectionItem>
                <apex:outputLabel value="City"/>
            </apex:pageblockSectionItem>            
            <apex:pageblockSectionItem>
                <apex:selectList size="1" value="{!city}" id="b">
                    <apex:selectOptions value="{!city}"/>
                </apex:selectList>
            </apex:pageblockSectionItem>                                     
        </apex:pageBlockSection>        
    </apex:pageBlock>
</apex:form>
</apex:page>

Controller Class :

public class ctlDepPickLst {
public String country {get;set;}
public String state {get;set;}
public String city {get;set;}

public List<SelectOption> getCountries()
{
    List<SelectOption> options = new List<SelectOption>();
    options.add(new SelectOption('None','--- Select ---'));        
    options.add(new SelectOption('US','United States'));
    options.add(new SelectOption('IN','India'));
    return options;
} 

public List<SelectOption> getStates()
{
    List<SelectOption> options = new List<SelectOption>();
    if(country == 'US')
    {       
        options.add(new SelectOption('CO','Colorado'));
        options.add(new SelectOption('NE','Nevada'));
        options.add(new SelectOption('TE','Texas'));
    }
    else if(country == 'IN')
    {       
        options.add(new SelectOption('BI','Bihar'));
        options.add(new SelectOption('KE','Kerala'));
        options.add(new SelectOption('MA','Manipur'));
    }
    else
    {
        options.add(new SelectOption('None','--- Select ---'));
    }      
    return options;
}       

public List<SelectOption> getCity()
{
    List<SelectOption> options2 = new List<SelectOption>();        
    if(state == 'CO')
    {
        options2.add(new SelectOption('BE','CO1'));
        options2.add(new SelectOption('MU','CO2'));
    }
    else
    {
        options2.add(new SelectOption('MI','test1'));
        options2.add(new SelectOption('AB','test2'));        
    }
    return options2;
}

}

1 Answer 1

0

believe its due to the variable and method name conflict on city..

you have a string variable called city

and you have a method called getCity to return the SelectOption list

to be consistent, if you change ur method name to getCities and use that in your VF page for city list, it should work.

Page :

<apex:selectOptions value="{!cities}"/>

Controller :

public List<SelectOption> getCities()
{
    List<SelectOption> options2 = new List<SelectOption>();        
    if(state == 'CO')
    {
        options2.add(new SelectOption('BE','CO1'));
        options2.add(new SelectOption('MU','CO2'));
    }
    else
    {
        options2.add(new SelectOption('MI','test1'));
        options2.add(new SelectOption('AB','test2'));        
    }
    return options2;
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .