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>