Answer - It doesn't seems possible to use the standard multi select pick list and hide values at UI.
Turnaround - You can create multi-select picklist using 2 selectLists one for Values Available and another for Selected Values to show only desired values from custom field at object ; Use two buttons in between those to select or deselect.
How do you do this ?
Following below Steps -
take all the values from the picklist using DescribeFieldResult() and filter necessary values. See below code to do this.
public List<SelectOption> getToSelectValues(){
List<SelectOption> countryUser = new List<SelectOption>();
Schema.DescribeFieldResult countryUserPkVal = Schema.ObjectName.FieldName.getDescribe();
List<Schema.PicklistEntry> ple = countryUserPkVal.getPicklistValues();
for(Schema.PicklistEntry f : ple){
//Your Logic to filter picklist values
if(!rightvalues.contains(f.getLabel()) )
countryUser.add(new SelectOption(f.getLabel(), f.getValue()));
}
return countryUser;
}
- For select button write below code
public PageReference selectclick(){
rightselected.clear();
for(String s : leftselected){
leftvalues.remove(s);
rightvalues.add(s);
}
return null;
}
To get selected values use below function
public List getSelectedValues(){
List options1 = new List();
List tempList = new List();
tempList.addAll(rightvalues);
tempList.sort();
for(String s : tempList)
options1.add(new SelectOption(s,s));
return options1;
}
for deSelect/unselect use below code
public PageReference unselectclick(){
leftselected.clear();
for(String s : rightselected){
rightvalues.remove(s);
leftvalues.add(s);
}
return null;
}
Finally to the VisualForce page, paste below code
<apex:panelGrid columns="3" id="panelGridId">
<apex:selectList id="sel1" value="{!leftselected}" multiselect="true" style="width:223px" size="4">
<apex:selectOptions value="{!ToSelectValues}" id="firstOptions" />
</apex:selectList>
<apex:panelGroup >
<br/>
<apex:image url="{!$Resource.selectImage}" height="20px" style="cursor:pointer;">
<apex:actionSupport event="onclick" action="{!selectclick}" reRender="panelGridId"/>
</apex:image>
<br/><br/>
<apex:image url="{!$Resource.unSelectImage}" height="20px">
<apex:actionSupport event="onclick" action="{!unselectclick}" reRender="panelGridId"/>
</apex:image>
</apex:panelGroup>
<apex:selectList id="sel2" value="{!rightselected}" multiselect="true" style="width:223px" size="4">
<apex:selectOptions value="{!SelectedValues}" />
</apex:selectList>
PS- do not forget to define the variables and initialize them before using
Public List<string> leftselected{get;set;}
Public List<string> rightselected{get;set;}
Set<string> leftvalues = new Set<string>();
Set<string> rightvalues = new Set<string>();