function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
VenkataRamanjaneyulu KattamuruVenkataRamanjaneyulu Kattamuru 

when i was trying remove the options selected I am unable to do anything & i am not getting any exception too..Please help.

My requirement is to add 7 remove
Hi all, 
when i was trying to remove theoptions which selected  I am unable to do anything(even logs are not coming in that method).
Please help ,

My code is shown below:

Apex-Class::

public class RealTimeSoql {
    public List<SelectOption> dropdowns {set;get;}
    public List<String> selectedoptions {set;get;}
    
    public Set<selectOption> displayList {set;get;}
    public List<string> removeList {set;get;}
    
    public  RealTimeSoql(){
        dropdowns         = new List<SelectOption>();
        selectedoptions = new List<String>();
        displayList        = new Set<selectOption>(); 
        removeList        = new List<string>();
        
        List<String> fields = new List<String>{'Name','Phone','Industry','Rating'};
            for(String s: fields){
                Selectoption sop = new SelectOption(s,s);
                dropdowns.add(sop);
            }
    }
    
    public void addOptions(){
        for(String s:selectedoptions){
            Selectoption dop = new SelectOption(s,s);
            displayList.add(dop);
        }
    }
    public void removeOptions(){
        System.debug('removeList:=============>'+ removeList);
        for(string s:removeList){
            Integer indexOfstring = selectedoptions.indexOf(s);
            String removed = selectedoptions.remove(indexOfstring);
        }
        System.debug('selectedoptions after remove:=============>'+ selectedoptions);
        addOptions();
        
    }

}

********************************************************************************************************
VFPage:

<apex:page controller="RealTimeSoql">
    <apex:form >
        <style>
            .box{
            width : 200px;
            height: 100px;
            }
        </style>
        <apex:pageBlock >
            
            <apex:selectList value="{!selectedoptions}" multiselect="true" styleClass="box">
                <apex:selectOptions value="{!dropdowns}"/>
            </apex:selectList>&nbsp;&nbsp;&nbsp;&nbsp;
            
            <apex:commandButton value="add"     action="{!addOptions}"/>    &nbsp;&nbsp;&nbsp;&nbsp;
              <apex:commandButton value="remove"  action="{!removeOptions}"/>    &nbsp;&nbsp;&nbsp;&nbsp; 
            
            <apex:selectList multiselect="true" styleClass="box" value="{!removeList}"   >
                <apex:selectOptions value="{!displayList}"/>
            </apex:selectList>    
             
        </apex:pageBlock>
    </apex:form>
</apex:page>

 
Best Answer chosen by VenkataRamanjaneyulu Kattamuru
Khan AnasKhan Anas (Salesforce Developers) 
Hi Venkata,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce:
<apex:page controller="MultiselectAddRemoveC" >
    <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:commandButton value="Add" action="{!selectclick}"/>    &nbsp;&nbsp;&nbsp;&nbsp;
                <apex:commandButton value="Remove" action="{!unselectclick}"/>    &nbsp;&nbsp;&nbsp;&nbsp; 
            </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>

Controller:
public class MultiselectAddRemoveC {
    
    Set<String> originalvalues = new Set<String>{'Name','Phone','Industry','Rating'};
        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 MultiselectAddRemoveC(){
        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;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Venkata,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce:
<apex:page controller="MultiselectAddRemoveC" >
    <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:commandButton value="Add" action="{!selectclick}"/>    &nbsp;&nbsp;&nbsp;&nbsp;
                <apex:commandButton value="Remove" action="{!unselectclick}"/>    &nbsp;&nbsp;&nbsp;&nbsp; 
            </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>

Controller:
public class MultiselectAddRemoveC {
    
    Set<String> originalvalues = new Set<String>{'Name','Phone','Industry','Rating'};
        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 MultiselectAddRemoveC(){
        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;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
VenkataRamanjaneyulu KattamuruVenkataRamanjaneyulu Kattamuru
Yes it is working as expected ,Great answer....Thank you  @Khan Anas