function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MalathanMalathan 

Initial selecton of SelectOption when generated through custom controller

I have a custom controller that is supplying a list of SelectOption values.  These values are in the form of year/month.  I want to set the initial selection of this list, not have it default to the first item.

 

If the items were directly entered into the Visual Force page, you just set the attribute selected="selected" on the appropriate option, but when generating via code, I do not see any way of setting this.

 

Any help would be appreciated

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

In the controller's constructor, initialize the variable that represents the field with the default selected value. For example:

 

<apex: page controller="theController">

 <apex: form>

  <apex: selectList value="{!theValue}">

   <apex: selectOptions value="{!theList}" />

  </apex: selectList>

 </apex: form>

</apex: page>

 

public class theController {
 public theController() {

  theValue = 'value 3';

 }

 private string theValue;

 public string getTheValue() {

  return theValue;

 }

 public void setTheValue(String value) {

  theValue = value;

 }

 public List<SelectOption> getTheList() {

  List<SelectOption> options = new List<SelectOption>();

  options.add(new SelectOption ('value 1','Value 1'));

  options.add(new SelectOption ('value 2','Value 2'));

  options.add(new SelectOption ('value 3','Value 3'));

  return options;

 }

}

All Answers

sfdcfoxsfdcfox

In the controller's constructor, initialize the variable that represents the field with the default selected value. For example:

 

<apex: page controller="theController">

 <apex: form>

  <apex: selectList value="{!theValue}">

   <apex: selectOptions value="{!theList}" />

  </apex: selectList>

 </apex: form>

</apex: page>

 

public class theController {
 public theController() {

  theValue = 'value 3';

 }

 private string theValue;

 public string getTheValue() {

  return theValue;

 }

 public void setTheValue(String value) {

  theValue = value;

 }

 public List<SelectOption> getTheList() {

  List<SelectOption> options = new List<SelectOption>();

  options.add(new SelectOption ('value 1','Value 1'));

  options.add(new SelectOption ('value 2','Value 2'));

  options.add(new SelectOption ('value 3','Value 3'));

  return options;

 }

}

This was selected as the best answer
metaforcemetaforce

Check out the example given in the Component Reference:

 

 

Example <!-- Page: --> <apex:selectList id="chooseColor" value="{!string}" size="1"> <apex:selectOption itemValue="red" itemLabel="Red"/> <apex:selectOption itemValue="white" itemLabel="White"/> <apex:selectOption itemValue="blue" itemLabel="Blue"/> </apex:selectList> /*** Controller ***/ public class chooseColor { String s = 'blue'; public String getString() { return s; } public void setString(String s) { this.s = s; } } HTML Output <select id="chooseColor" name="chooseColor" size="1"> <option value="red">Red</option> <option value="white">White</option> <option value="blue" selected="selected">Blue</option> </select>

 

AD

-----------------------------------------------------------------------------------

Mark it as an accepted solution if it works and help the coommunity