Example1: Creating picklist using <apex:selectoption> (this is the static way to place the items within picklist):
page:
<apex:page sidebar=”false” >
<apex:form >
Countries: <apex:selectList multiselect=”false” size=”1″>
<apex:selectoption itemvalue=”–None–” itemlabel=”–None–“></apex:selectoption>
<apex:selectoption itemvalue=”India” itemlabel=”India”></apex:selectoption>
<apex:selectoption itemvalue=”Australia” itemlabel=”Australia”></apex:selectoption>
<apex:selectoption itemvalue=”America” itemlabel=”America”></apex:selectoption>
</apex:selectList>
</apex:form>
</apex:page>
Example2: Getting all names of an account into a Picklist
page:
<apex:page sidebar=”false” controller=”accPickNamesDisplayClass”>
<apex:form >
<apex:selectList multiselect=”false” size=”1″>
<apex:selectOptions value=”{!accNames}”>
</apex:selectOptions>
</apex:selectList>
</apex:form>
</apex:page>
class:
public with sharing class accPickNamesDisplayClass {
List<selectOption> options = new List<selectOption>();
public List<selectOption> getAccNames() {
for(DataLoadTest__c acc : [select Id,name from DataLoadTest__c])
{
options.add(new selectOption(acc.name,acc.name));
}
return options;
}
}
Example3: Dependent Picklist Preparation using Controller:
In this scenario we can see how to generate a picklist dynamically and preparation of dependant picklists.
Here dependant picklist is will be populated using apex:actionsupport.
page:
<apex:page sidebar=”false” controller=”DependentClassabcd”>
<apex:form >
Departments: <apex:selectList value=”{!selDept}” multiselect=”false” size=”1″>
<apex:actionSupport event=”onchange” reRender=”out”/>
<apex:selectOptions value=”{!deptnames}”>
</apex:selectOptions>
</apex:selectList> <br/>
Employees: <apex:selectList multiselect=”false” id=”out” size=”1″>
<apex:selectOptions value=”{!empnames}”>
</apex:selectOptions>
</apex:selectList>
</apex:form>
</apex:page>
class:
public with sharing class DependentClassabcd {
public String selDept { get; set; }
List<selectOption> empOptions = new List<selectOption>();
public List<selectOption> getEmpnames() {
empOptions.clear();
empOptions.add(new selectOption(‘–None–‘,’–None–‘));
for(Employee__c objEmp : [select Id,name,department__c from Employee__c where department__r.name=:selDept]){
empOptions.add(new selectOption(objEmp.name,objEmp.name));
}
return empOptions;
}
List<selectOption> deptOptions = new List<selectOption>();
public List<selectOption> getDeptnames() {
deptOptions.add(new selectOption(‘–None–‘,’–None–‘));
for(Department__c objDept : [select name from Department__c]){
deptOptions.add(new selectOption(objDept.name,objDept.name));
}
return deptOptions;
}
}