How to use apex:selectOptions and prepare picklist values in Visualforce Page.
Hi,In this post i am using <apex:selectOptions >,<apex:selectRadio>,<apex:selectList> tags. Here from controller i am preparing pick-list values and set in visualforce page.
Controller:
public class SelectOptionController {
public List<SelectOption> countrieLst {get;set;}
public List<SelectOption> hobbiesLst {get;set;}
public List<SelectOption> genderLst {get;set;}
public String selectedCountry{get;set;}
public String selectedHobby {get;set;}
public String selectedGender {get;set;}
public String selectedValues {get;set;}
public SelectOptionController(){
countrieLst = new List<SelectOption>();
hobbiesLst = new List<SelectOption>();
genderLst = new List<SelectOption>();
countrieLst.add(new SelectOption('','--None--'));
countrieLst.add(new SelectOption('India','India'));
countrieLst.add(new SelectOption('China','China'));
countrieLst.add(new SelectOption('US','US'));
/* preparing picklist values in VF page.
hobbiesLst.add(new SelectOption('','--None--'));
hobbiesLst.add(new SelectOption('Swimming','Swimming'));
hobbiesLst.add(new SelectOption('Reading','Reading'));
hobbiesLst.add(new SelectOption('Cricket','Cricket'));
*/
genderLst.add(new SelectOption('Male','Male'));
genderLst.add(new SelectOption('Female','Female'));
}
public pageReference show(){
selectedValues = 'Selected country:'+selectedCountry +'-->Selected Hobbies:'+selectedHobby+' Gender: -->'+selectedGender;
return null;
}
}
Visualforce page:
<apex:page controller="SelectOptionController">
<apex:form >
<apex:commandbutton value="Show Values" action="{!show}"/>
<apex:pageBlock >
<apex:outputLabel > Countries : </apex:outputLabel>
<apex:selectList size="1" value="{!selectedCountry}">
<apex:selectOptions value="{!countrieLst }"/>
</apex:selectList> <br/>
<apex:outputLabel > Gender : </apex:outputLabel>
<apex:selectRadio value="{!selectedGender}">
<apex:selectOptions value="{!genderLst}"/>
</apex:selectRadio>
<apex:outputLabel > Hobbies :</apex:outputLabel>
<apex:selectList size="1" value="{!selectedHobby}">
<apex:selectOption itemLabel="--None--" itemvalue=""></apex:selectOption>
<apex:selectOption itemLabel="Swimming" itemvalue="Swimming"></apex:selectOption>
<apex:selectOption itemLabel="Reading" itemvalue="Reading"></apex:selectOption>
<apex:selectOption itemLabel="Cricket" itemvalue="Cricket"></apex:selectOption>
</apex:selectList>
</apex:pageBlock>
<apex:pageBlock title="selected values">
<apex:outputText value="{!selectedValues}" style="color:red"/>
</apex:pageBlock>
</apex:form>
</apex:page>
Test Class:
@isTest
Public class SelectOptionController_Test {
static testmethod void testSelectOptionController(){
//Test converage for the myPage visualforce page
PageReference pageref = Page.SelectOptionVFExample;
Test.setCurrentPageReference(pageref);
// create an instance of the controller
SelectOptionController sop= new SelectOptionController ();
sop.selectedCountry ='India';
sop.selectedHobby='Swimming';
sop.selectedGender ='Male';
sop.show();
system.assertEquals('India',sop.selectedCountry);
system.assertEquals('Swimming',sop.selectedHobby);
system.assertEquals('Male',sop.selectedGender);
}
}
out put:
8 comments:
Nice post...I look forward to reading more, and getting a more active part in the talks here, whilst picking up some knowledge as well..
Pass Box manufacturers
Thanku , very useful post.
This is the best explanation of setting picklist values in Apex. Very good job. Thank you.
nice blog...do have a look on Digital Marketing Agency in Bangalore India for professional Search Engine Optimization.
Visit digitalmarketingseo.in
thank u for sharing...do have a look on Google Analytics Services Provider. The process of analyzing the behavior of visitors to a Web site.
Visit digitalmarketingseo.in
thank u so much for sharing..nice article.. Have a look on Social Media Marketing Companies in Bangalore. Way to create a buzz for your website.
Visit digitalmarketingseo.in
have a proper optimization of keywords for all major search engines.Visit https://digitalmarketingseo.in/search-engine-opimization.html
Techforce services is a Salesforce Consulting Services in Australia Specialising in delivering end to end Salesforce solutions ,Consulting, Implementation DevOps partners in australia We deliver applications and services more rapidly and reliably, but it’s more than a methodology – it cuts to the very core.Salesforce Data Analytics let us help you become a data driven organisation and ensure your data is working hard for your business This includes implementi
Techforce services in Australia
Salesforce Consulting Services in Australia
Salesforce Staff Augmentation in Australia
Salesforce Data Analytics
DevOps Partners in Australia
Managed Projects Salesforce Australia
Post a Comment