Multiselect Picklists - El Toro - Find articles about Visualforce, Apex, Force.com and Salesforce in general

Print Preview

Multiselect Picklists

This sample demonstrates how to work with Multiselect picklists. Since you can select more than one option, the value is a list of string as demonstrated in this Visualforce page:

<apex:page controller="sampleCon">
    <apex:form>
        <apex:selectList value="{!countries}" multiselect="true">
            <apex:selectOptions value="{!items}"/>
        </apex:selectList><p/>

        <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>

and controller:

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;
	}
}

comments powered by Disqus

© El Toro . IT @ 2013
Andrés Pérez