November 08, 2014

Visualforce Page to select Multi pick list values

The scenario is, when we select multiple values from left hand side it should display in the right hand side and vice versa when we click left and right arrows.

Steps to create multi pick list values

Step1: Create Visual force Page

VF Page Name: multipicklistex

<apex:page controller="multiselectCon">

    <apex:form >
        <apex:panelGrid columns="3" id="abcd"> 
      
            <apex:selectList id="sel1" value="{!leftselected}" multiselect="true" style="width:100px" size="5">
                <apex:selectOptions value="{!unselectedvalues}" />
            </apex:selectList>
           
                <apex:panelGroup >
                    <br/>
                   <apex:image value="{!$Resource.Right}">
                        <apex:actionSupport event="onclick" action="{!selectclick}" reRender="abcd"/>
                    </apex:image>
                    <br/><br/>
                    <apex:image value="{!$Resource.Left}">
                        <apex:actionSupport event="onclick" action="{!unselectclick}" reRender="abcd"/>
                    </apex:image>
                </apex:panelGroup>
                
            <apex:selectList id="sel2" value="{!rightselected}" multiselect="true" style="width:100px" size="5">
                    <apex:selectOptions value="{!SelectedValues}" />
            </apex:selectList>
          
        </apex:panelGrid>
    </apex:form>
</apex:page>

Step 2: Write Controller for VF Page

Class Name: multiselectCon

public class multiselectCon {
 Set<String>originalvalues= new Set<String>{'Test1','Test2','Test3','Test4','Test5','Test6','Test7'};
    Public List<string> leftselected{get;set;}
    Public List<string> rightselected{get;set;}
    Set<string> leftvalues = new Set<string>();
    Set<string> rightvalues = new Set<string>();
    
    public multiselectCon (){
        leftselected = new List<String>();
        rightselected = new List<String>();
        leftvalues.addAll(originalValues);
    }
    
    public PageReference selectclick(){
        rightselected.clear();
        for(String s : leftselected){
            leftvalues.remove(s);
            rightvalues.add(s);
        }
        return null;
    }
    
    public PageReference unselectclick(){
        leftselected.clear();
        for(String s : rightselected){
            rightvalues.remove(s);
            leftvalues.add(s);
        }
        return null;
    }

    public List<SelectOption> getunSelectedValues(){
        List<SelectOption> options = new List<SelectOption>();
        List<string> tempList = new List<String>();
        tempList.addAll(leftvalues);
        tempList.sort();
        for(string s : tempList)
            options.add(new SelectOption(s,s));
        return options;
    }

    public List<SelectOption> getSelectedValues(){
        List<SelectOption> options1 = new List<SelectOption>();
        List<string> tempList = new List<String>();
        tempList.addAll(rightvalues);
        tempList.sort();
        for(String s : tempList)
            options1.add(new SelectOption(s,s));
        return options1;
    }
}

Step 3: Output



Step 4: Select the values present in the left hand side and click right arrow


Step 5: Now the values will display on right hand side.



Interview Questions:
  • What are Collections present in Apex?
  • Difference between List,Set,Map?
  • What is the use of Static Resources?
  • Difference between <apex:panelgrid> and <apex:panelgroup>
  • What is <apex:actionsupport>?When we have to use?
  • How to write <apex:selectoption> syntax for picklist?