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

Advertisements

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