+ Start a Discussion
arpit vijayvergiyaarpit vijayvergiya 

how to transfer data from one select list to another select list

User-added image
i want to transfer data from available list to selected list while clicking on add and remove button and also change the position by up down.. 

anyone can help plg...
thanx in advance
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Yummy,

Please check the below code.

VisualforcePage:
<apex:page controller="Collectionpractise">
   <style>
        .box{
            width: 150px;
            height:150px;
        }
    </style>
    <apex:form >
        <apex:pageBlock title="MultiSelect">
            <apex:panelGrid columns="3" id="one">
                <apex:selectList multiselect="true" styleClass="box" value="{!selected}">
                    <apex:selectOptions value="{!notselectedoptions}" />
                </apex:selectList>
                <apex:panelGrid columns="1">
                    <br/><br/>
                    <apex:commandButton value="Add" action="{!add}" reRender="one" />
                    <br/><br/>
                    <apex:commandButton value="Del" action="{!remove}" reRender="one" />
                </apex:panelGrid>
                <apex:selectList multiSelect="true" styleClass="box" value="{!remove}">
                    <apex:selectOptions value="{!selectedoptions}" />
                </apex:selectList>
            </apex:panelGrid>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Code:
public class Collectionpractise {
   //initializing datamembers
   //to avoid duplicate and maintain insertion order
    Public set<string> notselectedfields{set;get;}
    Public set<string> selectedfields{set;get;}
    //To display list of picklist values in the not selected options block by using selectoption predefined class
    Public List<selectoption> notselectedoptions{set;get;}
     //To display list of picklist values in the selected options block by using selectoption predefined class
    Public List<selectoption> selectedoptions{set;get;}
    //created a varablie to add and remove
    public list<string> selected{set;get;}
    public list<string> remove{set;get;}
 
    Public CollectionPractise(){
         //creating a list collecting and adding field to the list collection
        list<string> fields=new list<string>{'name','department','email','account','leadsource'};
        //memeory allocation for not selected fields
        notselectedfields=new set<string>();
        //memeory allocation for  selected fields
          selectedfields=new set<string>();
         //memory allocation for selected and removed metheds
        selected =new list<string>();
        remove=new list<string>();
         //adding the fields to memeory allocted to set variable called notselectedfields
            notselectedfields.addAll(fields);
        //memeory allocation for not selectedoption 
        notselectedoptions=new list<selectoption>();
        selectedoptions=new list<selectoption>();
       //Created a default label called none and adding to both notselected and selected options
        selectoption n= new selectoption('none','NONE');
        //adding none option to selected and not selected options list
           notselectedoptions.add(n);
           selectedoptions.add(n);
        getoptions();
    }
    public void getoptions(){
        notselectedoptions.clear();
        selectedoptions.clear();
        //creating the picklist value none
        selectoption n=new selectoption('none','None'); 
        //Checking the set collection notselectedfields, if the size is greater than zero executing the for loop 
        //number of times depending on the size of the set collection and adding the values to the notselectedoptions(list collection)
        // by creating a picklist value p1
        if(notselectedfields.size() > 0){
            for(string s : notselectedfields){
                selectoption p1=new selectoption(s,s);
                notselectedoptions.add(p1);   
            }
        }
            else{
                notselectedoptions.add(n);
            }
            if(selectedfields.size() > 0){
            for(string s1 : selectedfields){
                selectoption p2=new selectoption(s1,s1);
                selectedoptions.add(p2);
                
            }
                }
            else
            {
                selectedoptions.add(n);
            }
    }   
        public void add(){
           notselectedfields.removeAll(selected);
            selectedfields.addAll(selected);   
            getoptions();
        }
          public void remove(){
           selectedfields.removeAll(remove);
            notselectedfields.addAll(remove);   
              getoptions();
        }
        
    }

Please mark it as best answer if the information is informative.

Best Regards
Rahul Kumar