+ Start a Discussion
BalkishanBalkishan 

How to select city in a picklist by selecting a state in another picklist?

  How to select city in a picklist by selecting a state in another picklist?

 

    I have made an object called "State". There are three custom fields :

                                                                     1. State

                                                                     2. City

                                                                     3. Village

                                                                      in this object.

I have made  picklists for state, city and village . I want that whenever i select a state, city associated with this state got select. And also when i want that  whenever i select a city, Village associated with this city got select.If you have any solution please tell me.Thanks .

I have written code for that which is as follow:-

                                             For page

<apex:page controller="sampleCon">
<apex:form >
<apex:actionFunction name="Citysearch" action="{!getCities}" reRender="result" status="status1"/>
<apex:actionFunction name="Villagesearch" action="{!getVillages}" reRender="result" status="status2"/>

<apex:selectList value="{!State}" size="1" onclick="searchCity()" >
<apex:selectOptions value="{!items}" />
<apex:actionStatus startText="Searching..." stopText="Search Result" id="status1" >
</apex:actionstatus>
</apex:selectList>

<apex:selectList value="{!City}" size="1" onclick="searchVillage()">
<apex:selectOptions value="{!Cities}"/>
<apex:actionStatus starttext="searching..." stoptext="Search Complete" id="status2" >
</apex:actionStatus>
</apex:selectList>

<apex:selectList value="{!Village}" size="1">
<apex:selectOptions value="{!Villages}"/>
</apex:selectList>

</apex:form>
<script>
function searchCity(){
// alert('hiiiiiiiiiii');
getCities();
}
function searchVillage()
{
// alert('hello');
getVillages();
}
</script>
</apex:page>
For controller
public class sampleCon {
        String id;
        public StateCity__c State{get;set;}
        public StateCity__c City{get;set;}
        public StateCity__c Village{get;set;}
        
        public List<SelectOption> getItems() {
                    List<SelectOption> options = new List<SelectOption>();
                    options.add(new SelectOption('id','Rajasthan'));
                    options.add(new SelectOption('id','MP'));
                    return options;
                    }
        
        public List<SelectOption> getCities() {
                    id = ApexPages.currentPage().getParameters().get('id');
                    List<SelectOption> options = new List<SelectOption>();
                    options.add(new SelectOption('id','Ajmer'));
                    options.add(new SelectOption('id','Bhopal'));
                    return options;
                    }
        
        public List<SelectOption> getVillages() {
                    id = ApexPages.currentPage().getParameters().get('id');
                    List<SelectOption> options = new List<SelectOption>();
                    options.add(new SelectOption('id','Kishangarh'));
                    options.add(new SelectOption('id','Nimach'));
                    return options;
                    }               
}
SennahSennah

Have you tried to achieve this via Dependent Picklists?

BalkishanBalkishan

No i did not try this wia dependent picklist.

BalkishanBalkishan

Sorry sir i am unable to get my answer through your blog.Please sir can you tell me the right solution from code i have written?