How to user <apex:selectOptions > 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);
}
}
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);
}
}
No comments:
Post a Comment