2

I've created a custom multi-select picklist, in which values are also dynamic. But the left side values are not getting cleared, but it is getting added in right list.

Here is the Apex code :

public class multiSelect{

 /* variables for multiselect picklist */
public List<string> leftselected{get;set;}
public List<string> rightselected{get;set;}
public Set<string> leftvalues = new Set<string>();
public Set<string> rightvalues = new Set<string>();
/* variables for multiselect picklist */

//constructor
public multiSelect(){
    leftselected = new List<String>();
    rightselected = new List<String>();
}

public List<SelectOption> getunSelectedValues(){

        List<SelectOption> option = new List<SelectOption>();
        Set<string> leftvalues = new Set<string>();

        //options.add(new SelectOption('None','--None--'));
        Schema.DescribeFieldResult fieldResult = customobj__c.Services__c.getDescribe(); // my custom obj
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();

        for(Schema.PicklistEntry p : ple){
            leftvalues.add(p.getValue());
            option.add(new SelectOption(p.getValue(), p.getValue()));
        }

        system.debug('----leftvalues : '+leftvalues);

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

    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> getSelectedValues(){

        system.debug('@@@@rightvalues : '+rightvalues);

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

Visual Force code :

<apex:panelGrid columns="3" id="multibox">
                <apex:panelGroup >                       
                    <apex:selectList id="sel1" value="{!leftselected}" multiselect="true" style="width:100px" size="5">
                        <apex:selectOptions value="{!unselectedvalues}" />
                    </apex:selectList>
                </apex:panelGroup>

                <apex:panelGroup >
                    <apex:commandButton action="{!selectclick}" value="Add" rerender="multibox"/>
                    <br/><br/>
                    <apex:commandButton action="{!unselectclick}" value="Remove" rerender="multibox"/>
                </apex:panelGroup>

                <apex:panelGroup >
                    <apex:selectList id="sel2" value="{!rightselected}" multiselect="true" style="width:100px" size="5">
                        <apex:selectOptions value="{!SelectedValues}" />
                    </apex:selectList>
                </apex:panelGroup>

            </apex:panelGrid>
2

1 Answer 1

2

You issue is how you are populating the unSelectedValues. You are pulling the value from a describe without filtering the selected value out.

Also, you are using a local variable inlace of the class variable:

public List<SelectOption> getunSelectedValues(){
    ......
    Set<string> leftvalues = new Set<string>();
    .....
}

so your changes to leftValues class variable are not reflected in that method since it uses the local variable

Since I am not confident that all your code is present I am providing you with a set of properties and methods in which this all works rather than trying to modify your code and making assumptions.

Note When you load the page you can initialize the unSelected and selected values with the appropriate data from your describes

private Set<String> unSelectedNames = new Set<String>();
private Set<String> selectedNames = new Set<String>();
public List<String> selected {get; set; }
public List<String> unselected {get; set; }

public [className](){

    Schema.DescribeFieldResult fieldResult = customobj__c.Services__c.getDescribe(); // my custom obj
    List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
    for(Schema.PicklistEntry p : ple){
        unSelectedNames.add(p.getValue());

}


public List<SelectOption> selectedOptions { get
{
    List<String> sorted = new List<String>(selectedNames);
    sorted.sort();
    List<SelectOption> options = new List<SelectOption>();
    for (String s : sorted) 
    {
        options.add(new SelectOption(s, s));
    }
    return options;
    }
}

public List<SelectOption> unSelectedOptions { get
{
    List<String> sorted = new List<String>(unSelectedNames);
    sorted.sort();
    List<SelectOption> options = new List<SelectOption>();
    for (String s : sorted) {
        options.add(new SelectOption(s, s));
    }

    return options; 
    }   
}   

public PageReference selectName(){
    if (selected != null){
        for (String s: selected) {
            selectedNames.add(s);
            unSelectedNames.remove(s);
        }
    }       
    return null;
}

public PageReference selectAllNames(){
    if (unSelectedNames != null){
        for (String s : unselectedNames){
            selectedNames.add(s);
        }
        unselectedNames = new Set <String>();
    }       
    return null;
}

public PageReference unSelectName(){
    if (unselected != null){        
        for (String s: unselected) {
            unSelectedNames.add(s);
            selectedNames.remove(s);
        }
    }       
    return null;
}

public PageReference unSelectAllNames(){
    if (selectedNames != null){     
        for (String s: selectedNames) {
            unSelectedNames.add(s);
        }
        selectedNames = new Set <String>();
    }       
    return null;
}

Do not lazy load the left and right selected options

    public List<SelectOption> leftSelected { 
        get{

        List<SelectOption> options = new List<SelectOption>();
        for (String s : leftValues) {
            options.add(new SelectOption(s, s));
        }

        return options; 
        }   
    }  

Without seeing the getters for your select options it is hard to say more

1
  • Sorry I'll add the getters as well, as I forgot to did it earlier.
    – Nehal
    Commented May 10, 2017 at 4:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .