Showing posts with label SelectOption in Visualfoce. Show all posts
Showing posts with label SelectOption in Visualfoce. Show all posts

Saturday, May 23, 2015

How to Use Select options in Visualforce page?

How to Use Select options in Visualforce page?

Selectoption object specifies one of the possible values for visualforce select checkboxes, select list, select radio component.

It consist of label that is displayed to the end user, and the value that will be written to the controller.

Note:- The SelectOption can be displayed in the disable state.

Constructors:

We have 2 types of constructors in this class

Instantiating 

SelectOption one = bew SelectOpton (value(String), label(String), IsDisabled(true/false));

If isDisabled is true, the option is disabled . We can not select that value.


SelectOption one = new SelectOption(value, label) 
          Value as String
          Label as String


Methods:

             Ex: one.getLabel();
             output: : One
            Ex: one.setDisabled(false);


Example: 

Controller Class: 

public class SelectOptionExample {

    String[] countries = new String[]{};

    public PageReference test() {
        return null;
    }

    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('US','US'));
        options.add(new SelectOption('CANADA','Canada'));
        options.add(new SelectOption('MEXICO','Mexico'));
        return options;
    }

    public String[] getCountries() {
        return countries;
    }

    public void setCountries(String[] countries) {
        this.countries = countries;
    }
    

}


VF Page: 

<apex:page controller="SelectOptionExample">

   <apex:form >
        <apex:selectCheckboxes value="{!countries}">
            <apex:selectOptions value="{!items}"/>
        </apex:selectCheckboxes><br/>
        <apex:commandButton value="Test" action="{!test}" rerender="out" status="status"/>
    </apex:form>
    
    <apex:outputPanel id="out">
        <apex:actionstatus id="status" startText="testing...">
            <apex:facet name="stop">
                <apex:outputPanel >
                    <p>You have selected:</p>
                    <apex:dataList value="{!countries}" var="c">{!c}</apex:dataList>
                </apex:outputPanel>
            </apex:facet>
        </apex:actionstatus>
    </apex:outputPanel>
</apex:page>




Once you select the options and click on test it will display the selected checkboxes details.




That's it....



Monday, July 28, 2014

How to use apex:selectOptions and prepare picklist values in Visualforce Page.

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: 

save image




 
| ,