Saturday, 13 August 2011

Dynamic Multi Select Picklist on Vfpage.


Hai friends,
      I came with new post, This Post illustrates how to display Multiselect Picklist on Vfpage and how the values are populating into multiselect picklist.I am giving example like, we can field Type picklist on account pagelayout,now i am taking that field and populating that fields data in to multiselect picklist and selected values in to new field 'Selected values' custom field and while editing that record,loading values in to multiselect picklist.
                            
         
First we need to get the picklist values,for this we need to use describe object methods.
 Schema.DescribeFieldResult fieldResult = Schema.sObjectType.Account.fields.Type.getSObjectField().getDescribe();
   List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
   for( Schema.PicklistEntry f : ple)
   {       options .add(new SelectOption(f.getLabel(), f.getValue()));       originalvalues.add(f.getLabel());       system.debug('********PICK*******'+originalvalues);    }      
 we can use above code to get all the picklist values from Type field  from Account object.
 Now we need to load this values in to select options and  process the remaining functionality,i am giving total code for that please go through that you can understand the functionality
Controller
 public class multiselect1 {
    String strget='';     
public string Accid{get;set;}     
public String accName { get; set; }     
Set<string> originalvalues = new Set<string>();     
Public List<string> leftselected{get;set;}     
Public List<string> rightselected{get;set;}    
 Set<string> leftvalues = new Set<string>();     
Set<string> rightvalues = new Set<string>();    
 List<SelectOption> options = new List<SelectOption>();     
String selectedvalue='';     List<string> lstFinal = new List<string>();  
public List<SelectOption> getCountries() // getting picklist values and populatinting into multiselec picklist. 
{   
List<SelectOption> options = new List<SelectOption>();    
Schema.DescribeFieldResult fieldResult = Schema.sObjectType.Account.fields.Type.getSObjectField().getDescribe();    
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();       
for( Schema.PicklistEntry f : ple)
{       
options .add(new SelectOption(f.getLabel(), f.getValue()));       
originalvalues.add(f.getLabel());          
}          return options;  
public multiselect1()
{        
 getCountries();         
GetEdit();         
leftselected = new List<string>();        
 rightselected = new List<string>();         
leftvalues.addAll(originalValues);      
}     
public PageReference selectclick()
{        
 rightselected.clear();         
for(String s : leftselected){  // left side selected values             
leftvalues.remove(s);             
rightvalues.add(s); // adding to right side        
 }         return null;     
}     
public PageReference unselectclick(){  
   leftselected.clear();       
 for(String s : rightselected){ // right side selected values             
rightvalues.remove(s);             
leftvalues.add(s); // adding to left side.         
}         
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)             
ptions.add(new SelectOption(s,s));         
return options;     
   List<SelectOption> options1 = new List<SelectOption>();     
public List<SelectOption> getSelectedValues()
{         
options1.clear();        
List<string> tempList = new List<string>();        
 tempList.addAll(rightvalues);         tempList.sort();         
for(String s : tempList)         
options1.add(new SelectOption(s,s));         
 System.debug('**********selected Values'+options1 );           
for(integer i=0;i<options1.size();i++) {           
 if(i==0)               
selectedvalue = options1[i].getValue();            
else               
selectedvalue += ','+options1[i].getValue();             
}            
return options1;       
}      
public void Save()         
Account acc = new Account();         
acc.Name=accName;         
acc.Selected_Values__c=selectedvalue;         
insert acc;     
}     
ist<Account> selAcc = new list<Account>();     
public list<Account> GetEdit() 
{     
selAcc=[select Name,Selected_Values__c from Account where id=:ApexPages.currentPage().getParameters().get('id') and Selected_Values__c !=null ];           
strget =selAcc[0].Selected_Values__c;         
 list<String> lstSplitData =strget.split(',');          
AccName=selAcc[0].Name;         
 if(lstSplitData.size()!=null && lstSplitData.size()!= 0&& originalvalues.size()!=null && originalvalues.size()!=0 )          
{           
for(string sSelected : lstSplitData){              
  if(originalvalues.contains(sSelected.trim())){                    
lstFinal.add(sSelected);                
}            
}            
or(string sFinal:lstFinal){                
if(originalvalues.contains(sFinal.trim())){                   
originalvalues.remove(sFinal);                
}              
}          
}          
 rightvalues.addALL(lstFinal);         
 return selAcc;     
}   
}
Vfpage1:-
  <apex:page>
    <apex:form >
<apex:pageBlock tabStyle="Contact">
    <apex:outputLabel > Account Name:</apex:outputLabel><apex:inputText value="{!accName}"/>
        <apex:panelGrid columns="10" id="abcd">
        <apex:outputLabel > Type:</apex:outputLabel>
            <apex:selectList id="sel1" value="{!leftselected}" multiselect="true" style="width:150px" size="5">
                <apex:selectOptions value="{!unselectedvalues}" />
            </apex:selectList>
                <apex:panelGroup >
                    <br/>
                    <apex:image value="{!$Resource.multiselect}">
                        <apex:actionSupport event="onclick" action="{!selectclick}" reRender="abcd"/>
                    </apex:image>
                    <br/><br/>
<apex:image value="{!$Resource.multiunselect}">
                        <apex:actionSupport event="onclick" action="{!unselectclick}" reRender="abcd"/>
                    </apex:image>
                </apex:panelGroup>
            <apex:selectList id="sel2" value="{!rightselected}" multiselect="true" style="width:150px" size="5">
                <apex:selectOptions value="{!SelectedValues}" />
            </apex:selectList>
        </apex:panelGrid>
          <apex:commandButton value="Save" action="{!Save}"/>
  </apex:pageBlock>
  </apex:form>
  </apex:page>
 Execute this  by passing id to this page as Query string parameter.
please make sure before trying this,create a field "Selected_Values__c".
and upload select and unselect arrows in the static resources
Cheers,
Haribabu Amudalapalli

No comments:

Post a Comment