+ Start a Discussion
Navneeth RajNavneeth Raj 

How to write logic so that none option won't come to the non selected places?

Apex Class
public class MultiselectExample {
    public Set<String> nscities         {Set;get;}
    public Set<String> scities          {set;get;}
    public List<SelectOption> soptions  {set;get;}
    public List<SelectOption> nsoptions   {set;get;}
    public List<String> selected        {set;get;}
    public List<String> removed     {set;get;}
      public MultiselectExample(){
        nscities=new Set<String>{'Hyd','Ban','Pune','Che'};
        scities=new Set<String>();
        soptions=new List<SelectOption>();
        nsoptions=new List<SelectOption>();
        selected=new List<String>();
        removed=new List<String>();
        calloptions();
    }
    public void callOptions(){
        nsoptions.clear();
        soptions.clear();
        if(scities.size()<=0){
            SelectOption p=new SelectOption('none','-None-');
            soptions.add(p);
        }else{
            for(String s1:scities){
                SelectOption op1=new SelectOption(s1,s1);
                soptions.add(op1);
            }
        }
         if(nscities.size()<=0){
            SelectOption p1=new SelectOption('none','-None-');
            nsoptions.add(p1);
        }else{
            for(String s2:nscities){
                SelectOption op2=new SelectOption(s2,s2);
                nsoptions.add(op2);
            }
        }
    }
    public void addElements(){
        nscities.removeAll(selected);
        scities.addAll(selected);
        calloptions();
    }
    public void removeElements(){
       scities.removeAll(removed);
       nscities.addAll(removed);
        calloptions();
    }
}


VIsualForce
<apex:page controller="MultiselectExample">
    <style>
        .mylist{
            width:100px;
            height:80px;
        }
    </style>
    <apex:form >
        <apex:pageBlock title="MultiSelect Example" >
            <apex:panelGrid columns="3" id="one">
                <apex:selectList multiselect="true" value="{!selected}" styleclass="mylist">
                    <apex:selectOptions value="{!nsoptions}" />
                </apex:selectList>
                <apex:panelGrid columns="1">
                    <apex:commandButton value="Add" action="{!addElements}" reRender="one" />    <br/>
                    <apex:commandButton value="Del" action="{!removeElements}" reRender="one" />
                </apex:panelGrid>
                <apex:selectList multiselect="true" value="{!removed}" styleclass="mylist">
                    <apex:selectOptions value="{!soptions}" />
                </apex:selectList>   
            </apex:panelGrid>
        </apex:pageBlock>
    </apex:form>
</apex:page>


The output for this is as below

User-added image
But if i selected none at right & click delete then it is added to the left as below.
User-added image
So how to write rewrite code so that when i select none at the right & delete it, then it should not add to the left (i.e to the non-selected options)
KaranrajKaranraj
Naveenth - Please find the updated code controller class code below, which will not add none option to the left side multipicklist, when you select and delete the none at the right side option.
public class MultiselectExample {
    public Set<String> nscities         {Set;get;}
    public Set<String> scities          {set;get;}
    public List<SelectOption> soptions  {set;get;}
    public List<SelectOption> nsoptions   {set;get;}
    public List<String> selected        {set;get;}
    public List<String> removed     {set;get;}
      public MultiselectExample(){
        nscities=new Set<String>{'Hyd','Ban','Pune','Che'};
        scities=new Set<String>();
        soptions=new List<SelectOption>();
        nsoptions=new List<SelectOption>();
        selected=new List<String>();
        removed=new List<String>();
        calloptions();
    }
    public void callOptions(){
        nsoptions.clear();
        soptions.clear();
        if(scities.size()<=0){
            SelectOption p=new SelectOption('none','-None-');
            soptions.add(p);
        }else{
            for(String s1:scities){
                SelectOption op1=new SelectOption(s1,s1);
                soptions.add(op1);
            }
        }
         if(nscities.size()<=0){
            SelectOption p1=new SelectOption('none','-None-');
            nsoptions.add(p1);
        }else{
            for(String s2:nscities){
                if(s2 != 'none'){ //Added If conidtion
                 SelectOption op2=new SelectOption(s2,s2);
                 nsoptions.add(op2);
                }
            }
        }
    }
    public void addElements(){
        nscities.removeAll(selected);
        scities.addAll(selected);
        calloptions();
    }
    public void removeElements(){
       scities.removeAll(removed);
       nscities.addAll(removed);
        calloptions();
    }
}

Thanks,
Karanraj