SelectOption Class
Namespace
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:
1public class sampleCon {
2
3 String[] countries = new String[]{};
4
5 public PageReference test() {
6 return null;
7 }
8
9 public List<SelectOption> getItems() {
10 List<SelectOption> options = new List<SelectOption>();
11 options.add(new SelectOption('US','US'));
12 options.add(new SelectOption('CANADA','Canada'));
13 options.add(new SelectOption('MEXICO','Mexico'));
14 return options;
15 }
16
17 public String[] getCountries() {
18 return countries;
19 }
20
21 public void setCountries(String[] countries) {
22 this.countries = countries;
23 }
24
25}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:
1<apex:page controller="sampleCon">
2 <apex:form>
3 <apex:selectCheckboxes value="{!countries}">
4 <apex:selectOptions value="{!items}"/>
5 </apex:selectCheckboxes><br/>
6 <apex:commandButton value="Test" action="{!test}" rerender="out" status="status"/>
7 </apex:form>
8 <apex:outputPanel id="out">
9 <apex:actionstatus id="status" startText="testing...">
10 <apex:facet name="stop">
11 <apex:outputPanel>
12 <p>You have selected:</p>
13 <apex:dataList value="{!countries}" var="c">{!c}</apex:dataList>
14 </apex:outputPanel>
15 </apex:facet>
16 </apex:actionstatus>
17 </apex:outputPanel>
18</apex:page>SelectOption Constructors
The following are constructors for SelectOption.
SelectOption(value, label)
Signature
public SelectOption(String value, String label)
SelectOption(value, label, isDisabled)
Signature
public SelectOption(String value, String label, Boolean isDisabled)
Parameters
- value
- Type: String
- The string that is returned to the Visualforce controller if the option is selected by a user.
- label
- Type: String
- The string that is displayed to the user as the option choice.
- isDisabled
- Type: Boolean
- If set to true, the option can’t be selected by the user but can still be viewed.
SelectOption Methods
The following are methods for SelectOption. All are instance methods.
getDisabled()
Signature
public Boolean getDisabled()
Return Value
Type: Boolean
Usage
If isDisabled is set to true, the user can view the option, but cannot select it. If isDisabled is set to false, the user can both view and select the option.
getEscapeItem()
Signature
public Boolean getEscapeItem()
Return Value
Type: Boolean
Usage
If itemEscaped is set to true, sensitive HTML and XML characters are escaped in the HTML output generated by this component. If itemEscaped is set to false, items are rendered as written.
getLabel()
Signature
public String getLabel()
Return Value
Type: String
getValue()
Signature
public String getValue()
Return Value
Type: String
setDisabled(isDisabled)
Signature
public Void setDisabled(Boolean isDisabled)
Parameters
- isDisabled
- Type: Boolean
Return Value
Type: Void
Usage
If isDisabled is set to true, the user can view the option, but cannot select it. If isDisabled is set to false, the user can both view and select the option.
setEscapeItem(itemsEscaped)
Signature
public Void setEscapeItem(Boolean itemsEscaped)
Parameters
- itemsEscaped
- Type: Boolean
Return Value
Type: Void
Usage
If itemEscaped is set to true, sensitive HTML and XML characters are escaped in the HTML output generated by this component. If itemEscaped is set to false, items are rendered as written.
setLabel(label)
Signature
public Void setLabel(String label)
Parameters
- label
- Type: String
Return Value
Type: Void
setValue(value)
Signature
public Void setValue(String value)
Parameters
- value
- Type: String
Return Value
Type: Void