Type 6:Usage of Multi select Dependent picklist in vf & Apex

Featured

Dependent MultiSelect PickList

<apex:page controller="dependentone" sidebar="false" showHeader="false">
  <apex:form >
     <b> Dependent MultiSelect PickList</b>
     <br/>
     Select The Picklist Value(s):
    <apex:selectList value="{!myVal}" size="2" multiselect="true" title="Select The Picklist Value(s)">
       <apex:selectOption itemvalue="1s" itemlabel="Ones"/>
       <apex:selectOption itemvalue="2s" itemlabel="Twos"/>
       <apex:selectOption itemvalue="3s" itemlabel="Threes"/>
       <apex:selectOption itemvalue="4s" itemlabel="Fours"/>
       <apex:selectOption itemvalue="5s" itemlabel="Fives"/>
       <apex:actionSupport event="onchange" reRender="one"/>
   </apex:selectList>
   <br/>
   <apex:outputpanel id="one"> 
     <b>The selected value(s) are: </b>
      <apex:selectList size="1" value="{!selectedDepval}" >
        <apex:selectOptions value="{!depval}"/>
      </apex:selectList>
   </apex:outputpanel>
 </apex:form>
</apex:page>

Apex Code/Controller:

public class dependentone {
public String selecteddepval { get; set; }
public String myVal { get; set; }
 public List<SelectOption> getdepval() {
 List<SelectOption> options = new List<SelectOption>();
 if(myval==null)
 return null;
 else
 {
  if(myVal.contains('1s')){
 options.add(new SelectOption('One','1'));
 options.add(new SelectOption('Two','2'));
 options.add(new SelectOption('Three','3'));
 options.add(new SelectOption('Four','4'));
 options.add(new SelectOption('Five','5'));
 }

 if(myVal.contains('2s')){
 options.add(new SelectOption('Ten','10'));
 options.add(new SelectOption('Twenty','20'));
 options.add(new SelectOption('Thirty','30'));
 options.add(new SelectOption('Fourty','40'));
 options.add(new SelectOption('Fifty','50'));
 }
if(myVal.contains('3s')){
 options.add(new SelectOption('One Hundreed','100'));
 options.add(new SelectOption('Two Hundreed','200'));
 options.add(new SelectOption('Three Hundreed','300'));
 options.add(new SelectOption('Four Hundreed','400'));
 options.add(new SelectOption('Five Hundreed','500'));
 }
 if(myVal.contains('4s')){
 options.add(new SelectOption('One Thousand','1000'));
 options.add(new SelectOption('Two Thousand','2000'));
 options.add(new SelectOption('Three Thousand','3000'));
 options.add(new SelectOption('Four Thousand','4000'));
 options.add(new SelectOption('Five Thousand','5000'));
 }
 if(myVal.contains('5s')){
 options.add(new SelectOption('Ten Thousand','1000'));
 options.add(new SelectOption('Twenty Thousand','2000'));
 options.add(new SelectOption('Thrty Thousand','3000'));
 options.add(new SelectOption('Fourty Thousand','400,00'));
 options.add(new SelectOption('Fivty Thousand','500,00'));
 }
 }
 return options;

 }
}
Type 6:Usage of Multi select Dependent picklist in vf & Apex

Type 6:Usage of Multi select Dependent picklist in vf & Apex

Type 5:Usage of Dynamic picklist in vf & Apex


<apex:page controller="dynamicpickone" sidebar="false" showHeader="false">

<apex:form >
 <apex:selectList id="selectpick" size="1" value="{!selectedValue}">
 <apex:selectOptions value="{!pickOptions}"/>
 <apex:actionSupport event="onchange" reRender="one"/>
 </apex:selectList>

<apex:outputPanel id="one">Selected Value is:
 <apex:outputText value="{!selectedValue}" />
 </apex:outputPanel>
 </apex:form>
 </apex:page>

Controller:

public class dynamicpickone{
//Constructor
public dynamicpickone() {

Parent = new Parent__c();
pickOptions = new List<SelectOption>();

// Use DescribeFieldResult object to retrieve commodities field.
// statusFieldDescription=sFD
Schema.DescribeFieldResult sFD = Parent__c.commodities__c.getDescribe();

// For each picklist value, create a new select option
//picklistEntry=plE
for (Schema.Picklistentry plE:sFD.getPicklistValues()){

pickOptions.add(new SelectOption(plE.getValue(),plE.getLabel()));

// obtain and assign default value
//if (picklistEntry.defaultValue){
// Parent.commodities__c = pickListEntry.getValue();
// }
}
}

Type 5:Usage of Dynamic picklist in vf & Apex

Type 5:Usage of Dynamic picklist in vf & Apex

Type 4:Usage of picklist in vf & Apex

Some times we need to display picklist in Vf page which is of type picklist in Sobject. Acutally we are not displaying the picklist values but then picklist record values which may have redundency…lets take an example .


<apex:page controller="picklistexamp2" showHeader="false" sidebar="false">

 <apex:form >
  <apex:selectList size="1" value="{!selectedValue}">
   <apex:selectOptions value="{!values}"/>
   <apex:actionSupport event="onchange" reRender="one"/>
  </apex:selectList><br/>
  <apex:outputLabel id="one"><b> The selected value is : {!selected     Value}<b></apex:outputlabel>
  </apex:form>
</apex:page>

Controller


public class picklistexamp2{
public List<selectoption> getValues() {
List< selectOption> ls= new list<selectOption>();
for(Parent__c p:[select commodities__c from Parent__c]){
ls.add(new selectOption(p.commodities__c,p.commodities__c));
}
return ls;
}
public String selectedValue { get; set; }
}

Out put: Here it is displaying the totla number of picklist values present in the records but not the picklist values present in it.

 one
To Resolve this we Use Map , By Using Map We can get all unique values in the map and we can store in list to display finally.
and the for this is


List< selectOption> ls= new list<selectOption>();

map <string,selectoption> map1= new map<string,selectoption>();

for(Parent__c p:[select commodities__c from Parent__c]){
map1.put(p.commodities__c, new selectoption(p.commodities__c,p.commodities__c));
}
ls=map1.values();
return ls;
}

one

 

Type 3:Usage of picklist in vf & Apex

Exapme: Creating Picklist In Visualforce Page itself

<apex:page controller="zvf5" sidebar="false" showHeader="false">
 <apex:form >
   <apex:selectList value="{!myVal}" size="3" multiselect="true">
     <apex:selectOption itemvalue="One" itemlabel="1"/>
     <apex:selectOption itemvalue="Two" itemlabel="2"/>
     <apex:selectOption itemvalue="Three" itemlabel="3"/>
     <apex:selectOption itemvalue="Four" itemlabel="4"/>
     <apex:selectOption itemvalue="Five" itemlabel="5"/>
        <apex:actionSupport event="onchange" reRender="one"/>
   </apex:selectList>
   <br/>
   <apex:outputLabel id="one">
      <b>The selected value is : {!myVal} </b>
   </apex:outputlabel>
   </apex:form>
 </apex:page>

Controller:

public class zvf5 {

public String myVal { get; set; }
}

one

Type 2:Usage of picklist in vf & Apex

Example: Creating Picklist in Visualforce page (In Sobject its not Picklist)
Lets take an example that we have one Sobject Employee__c and the Field name  is EmpName(std field)

<apex:page controller=”vfpage2″ sidebar=”false” showHeader=”false”>
<apex:form >
<apex:selectList size=”1″ value=”{!selectedValue}”>
<apex:selectOptions value=”{!values}”/>
<apex:actionSupport event=”onchange” reRender=”one”/>
</apex:selectList>
<br/>
<apex:outputLabel id=”one”> <b> The selected Employee Name is : {!selectedValue}</apex:outputlabel>
</apex:form>
</apex:page>

Controller:
public class vfpage2 {
public String selectedValue { get; set; }
public List<selectoption> getValues() {
List< selectOption> ls= new list<selectOption>();
List<Employee__c> emp=[select empName from Employee__c];
for(Employee__c e:emp){
ls.add(new selectOption(e.empName,e.empName));
}
return ls;
}

}
OutPut:
one

Type 1:Usage of picklist in vf & Apex

Picklist fields contain a list of one or more items from which a user chooses a single item. They display as drop-down lists in the Salesforce user interface. One of the items can be configured as the default item.For more Details http://www.salesforce.com/us/developer/docs/api/Content/field_types.htm

Example:Displaying the picklist in Visulaforce page.

<apex:page controller=”vfpage1″ sidebar=”false” showHeader=”false”>
<apex:form >
<apex:selectList value=”{!myVal}” multiselect=”true”>
<apex:selectOptions value=”{!myOptions}”/>
<apex:actionSupport event=”onchange” reRender=”one”/>
</apex:selectList>
<br/>
<apex:outputLabel id=”one”> <b>The selected value is : {!myVal} <b></apex:outputlabel>
</apex:form>
</apex:page>
Controller:
public class vfpage1 {
public String myVal { get; set; }
public List<selectoption> getMyOptions() {
List< selectOption> ls= new list<selectOption>();
selectOption s0=new selectOption(‘Null’,’-Null-‘);
selectOption s1=new selectOption(‘1′,’One’);
selectOption s2=new selectOption(‘2′,’Two’);
selectOption s3=new selectOption(‘3′,’Three’);
ls.add(s0);
ls.add(s1);
ls.add(s2);
ls.add(s3);
return ls;
}

}
OutPut:
one