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
Dman100Dman100 

display selected value on selectList

If a user has made a selection from a selectList, is there a way to display the selected value as the default?

 

Initially, when the user comes to the page, I have SelectOption set as follows:

 

options.add(new SelectOption('','-- Please select --')); for (string s : strSundays) { options.add(new SelectOption(s,s)); } return options;

 

Upon returning to the page for the specified record, I want to show the selected value as the default instead of '-- Please select --'

 

Here is the code used for the selection list:

 

VF code:

 

 

<apex:selectList multiselect="false" size="1" value="{!workweek}" > <apex:selectOptions value="{!items}" /> </apex:selectList>

 

Controller class code:

 

string workweek; public List<SelectOption> getItems() { List<SelectOption> options = new List<SelectOption>(); List<date> dates = getSundays(); List<string> strSundays = new List<string>(); for (date d: dates) { strSundays.add(string.valueof(d)); } options.add(new SelectOption('','-- Please select --')); for (string s : strSundays) { options.add(new SelectOption(s,s)); } return options; } public string getWorkWeek() { return workweek; } public void setWorkWeek(string workweek) { this.workweek = workweek; }

Thanks.

asadimasadim
In your controller's constructor just set workweek to the value you want to show as default on your page.
jeffdonthemic2jeffdonthemic2

Take a look at the following blog post. There is code for multiple picklists where I set the selected value.

 

Dependent Multilevel Selectlists 

 

Jeff Douglas
Appirio, Inc.
http://blog.jeffdouglas.com 

yvk431yvk431

the string "workweek"  should hold the selected value as you are passing the list values using "item"

 

All you have to do is to assign the selected value from DB to the property "workweek".