I have a picklist which holds the country names and based on the name selected I want to populate the states. I have given a piece of code done by me **but I am not able to get it working for the states picklist
Controller:
public String fanCountry_Region { get; set; }
public List<SelectOption> StateCodeList=new List<SelectOption>();
public String selectedCountryCode {get; set;}
public void fetchfanvalues() {
fan = new fan__c();
fan=[SELECT id, Email__c,state__c
FROM fan__c
WHERE Encrypted_ID__c=:encryptedfanID];
if (fan != NULL)
{
selectedCountryCode=fan.Country_Region__c; //get the selected code
system.debug('selectedCountryCode Is :'+selectedCountryCode);41
}
}
public List<SelectOption> getCountryCodes() {
if(countryCodeList.isEmpty()) {
countryCodeList=new List<SelectOption>();
List<Countries__c> allCountries = new List<Countries__c>();
allCountries = [SELECT Name,CountryCode__c
FROM Countries__c];
allCountries.sort();
List<Countries__c> cList=new List<Countries__c>([select CountryCode__c
from Countries__c
where
CountryCode__c=:selectedCountryCode]);
if (cList.size() > 0) {
for(Countries__c country : allCountries) {
countryCodeList.add( new SelectOption( country.CountryCode__c, country.Name ) );
}
} else {
countryCodeList.add(new SelectOption('--Select--', '--Select--'));
}
}
return countryCodeList;
}
public List<SelectOption> getStateCodeList() {
for(States__c state :States__c.getAll().values()) {
if(state.CountryCode__c == selectedCountryCode) {
stateCodeList.add(new SelectOption(state.CountryCode__c, state.StateCode__c));
}
}
return StateCodeList;
}
VF Page:
<apex:selectList value="{!fanCountry_Region}" size="1">
<apex:selectOption itemValue=" " itemLabel="--Select--"></apex:selectOption>
<apex:selectOptions value="{!countryCodes}"></apex:selectOptions>
</apex:selectList>
<apex:selectList value="{!selectedState}" size="1">
<apex:selectOption itemValue=" " itemLabel="--Select--"></apex:selectOption>
<apex:selectOptions value="{!StateCodeList}"></apex:selectOptions>
</apex:selectList>