Hi,
Today, i got a requirement for creating a dependant picklist which gets its values from custom setting. I thought of sharing it with you all.
In my example, i am selecting name of countries and its associated city from custom settings.
Steps :
1) Create a new list type custom setting, with only one text field named 'City' in my case.
2) Click on manage in the custom setting, and create a new record.
Name of the custom setting record should be name of the country.
City field contains name of the cities for that country, separated by ';'
3) Code for page :
<apex:page controller="CustomSettings_controller" >
<apex:pageBlock >
<apex:form >
<apex:actionFunction name="rerenderStates" rerender="ListState" >
<apex:param name="firstParam" assignTo="{!country}" value="" />
</apex:actionFunction>
<apex:pageblocksection >
Country <apex:selectList id="ListCountry" value="{!country}" size="1" onchange="rerenderStates(this.value)">
<apex:selectOptions value="{!countrylist}"></apex:selectOptions>
</apex:selectList>
</apex:pageblocksection>
<apex:pageblocksection >
State <apex:selectList id="ListState" value="{!states}" size="1">
<apex:selectOptions value="{!statelist}"></apex:selectOptions>
</apex:selectList>
</apex:pageblocksection>
</apex:form>
</apex:pageBlock>
</apex:page>
4) code for controller :
public class CustomSettings_controller {
Map<String, countrystate__c> c = countrystate__c.getall();
public list<selectoption> getStateList() {
list<selectoption> s = new list<selectoption> ();
s.add(new selectoption('','Select one'));
if(country==null){
country='';
}
else
if(c.containskey(country)){
s.clear();
string sm = c.get(country).States__c;
for(string st: sm.split(';'))
s.add(new selectoption(st,st));
}
return s;
}
public List<SelectOption> getCountrylist() {
List<SelectOption> country = new List<SelectOption>();
country.add(new selectoption('India','---SELECT ONE---'));
list<string> countryname= new list<string>();
countryname.addall(c.keyset());
for(string s : countryname){
country.add(new selectoption(s,s));
}
return country;
}
public String states { get; set; }
public String country{ get; set;}
}
Final Outcome :
use explaining Naming convention so it will be more clear for me
ReplyDelete