+ Start a Discussion
Jeffrey ZhangJeffrey Zhang 

Multiselect selectlist with preset selected values.

We have a visualforce 'edit' page for a custom object. The variable is stored in a String (eg: " a;b;c"). I want to be able to pull the saved values and preselect them on a multi selectList so users don't have to reselect the old values when editing. 

value = [select ....];

<apex:selectList value="{!value}" size="3" multiselect="true" label="blah" id="blah" >
                        <apex:selectOptions value="{!options}" />
                    </apex:selectList>

I've tried string format and String[] list format for 'value' and neither gets me any default selected values for this select list. 
Best Answer chosen by Jeffrey Zhang
Alexander TsitsuraAlexander Tsitsura
Hello Jeffrey,

If you want to prefill a multi-select, you need use String[] list. Please, look at the code below:
 
// -------------------------
// Page
<apex:page controller="MultiselectController">
  <apex:form >
    <apex:selectList value="{!value}" size="3" multiselect="true" label="blah" id="blah" >
        <apex:selectOptions value="{!options}" />
    </apex:selectList>
  </apex:form>
</apex:page>
//--------------------------
// Controller
public class MultiselectController {
    public List<SelectOption> options { get; set; }
    public String[] value { get; set; }
    
    public Temp() {
        value = new String[] { '1', '2' };
        options  = new List<SelectOption> {
            new SelectOption('1', '1'),
            new SelectOption('2', '2'),
            new SelectOption('3', '3'),
            new SelectOption('4', '4')
        };
    }
}
As you can see, value "1" and "2" selected.
User-added image


Thanks,
Alex 

All Answers

Alexander TsitsuraAlexander Tsitsura
Hello Jeffrey,

If you want to prefill a multi-select, you need use String[] list. Please, look at the code below:
 
// -------------------------
// Page
<apex:page controller="MultiselectController">
  <apex:form >
    <apex:selectList value="{!value}" size="3" multiselect="true" label="blah" id="blah" >
        <apex:selectOptions value="{!options}" />
    </apex:selectList>
  </apex:form>
</apex:page>
//--------------------------
// Controller
public class MultiselectController {
    public List<SelectOption> options { get; set; }
    public String[] value { get; set; }
    
    public Temp() {
        value = new String[] { '1', '2' };
        options  = new List<SelectOption> {
            new SelectOption('1', '1'),
            new SelectOption('2', '2'),
            new SelectOption('3', '3'),
            new SelectOption('4', '4')
        };
    }
}
As you can see, value "1" and "2" selected.
User-added image


Thanks,
Alex 
This was selected as the best answer
Jeffrey ZhangJeffrey Zhang
thank you.