Newer Version Available
SelectOption Class
Namespace
System
SelectOption consists of a label that is displayed to the end user, and a value that is returned to the controller if the option is selected. A SelectOption can also be displayed in a disabled state, so that a user cannot select it as an option, but can still view it.
Instantiation
-
1SelectOption option = new SelectOption(value, label, isDisabled);where value is the String that is returned to the controller if the option is selected by a user, label is the String that is displayed to the user as the option choice, and isDisabled is a Boolean that, if true, specifies that the user cannot select the option, but can still view it.
-
1SelectOption option = new SelectOption(value, label);where value is the String that is returned to the controller if the option is selected by a user, and label is the String that is displayed to the user as the option choice. Because a value for isDisabled is not specified, the user can both view and select the option.
Example
The following example shows how a list of SelectOptions objects can be used to provide possible values for a selectCheckboxes component on a Visualforce page. In the following custom controller, the getItems method defines and returns the list of possible SelectOption objects:
1swfobject.registerObject("clippy.codeblock-2", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public class sampleCon {
18
19 String[] countries = new String[]{};
20
21 public PageReference test() {
22 return null;
23 }
24
25 public List<SelectOption> getItems() {
26 List<SelectOption> options = new List<SelectOption>();
27 options.add(new SelectOption('US','US'));
28 options.add(new SelectOption('CANADA','Canada'));
29 options.add(new SelectOption('MEXICO','Mexico'));
30 return options;
31 }
32
33 public String[] getCountries() {
34 return countries;
35 }
36
37 public void setCountries(String[] countries) {
38 this.countries = countries;
39 }
40
41} In the following page markup, the <apex:selectOptions> tag uses the getItems method from the controller above to retrieve the list of possible values. Because <apex:selectOptions> is a child of the <apex:selectCheckboxes> tag, the options are displayed as checkboxes:
1swfobject.registerObject("clippy.codeblock-3", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 <apex:page controller="sampleCon">
18 <apex:form>
19 <apex:selectCheckboxes value="{!countries}">
20 <apex:selectOptions value="{!items}"/>
21 </apex:selectCheckboxes><br/>
22 <apex:commandButton value="Test" action="{!test}" rerender="out" status="status"/>
23 </apex:form>
24 <apex:outputPanel id="out">
25 <apex:actionstatus id="status" startText="testing...">
26 <apex:facet name="stop">
27 <apex:outputPanel>
28 <p>You have selected:</p>
29 <apex:dataList value="{!countries}" var="c">{!c}</apex:dataList>
30 </apex:outputPanel>
31 </apex:facet>
32 </apex:actionstatus>
33 </apex:outputPanel>
34</apex:page>