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....



 
| ,