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

Advertisements

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