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.
SelectOption 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.
SelectOption 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.
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:
public class sampleCon { 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; } }
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:
<apex:page controller="sampleCon"> <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>
The following are constructors for SelectOption.
public SelectOption(String value, String label)
public SelectOption(String value, String label, Boolean isDisabled)
The following are methods for SelectOption. All are instance methods.
public Boolean getDisabled()
Type: Boolean
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.
public Boolean getEscapeItem()
Type: Boolean
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.
public String getLabel()
Type: String
public String getValue()
Type: String
public Void setDisabled(Boolean isDisabled)
Type: Void
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.
public Void setEscapeItem(Boolean itemsEscaped)
Type: Void
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.
public Void setLabel(String label)
Type: Void
public Void setValue(String value)
Type: Void