Dependent Picklist in Visualforce Page

In this part, I am going to explain how to create a dependent picklist via some code.
You can do this stuff without coding i.e. (point and click) tool for a sObject record.

There are two terms that depict about FieldDependencies.
1.  Controlling Field
2.  Dependent Field

Field dependencies are the filters that allow us to change the content of a picklist based on a value of another field.

Controlling field that controls available values in one or more corresponding dependent picklist.

A dependent field display values based on a value selected in the corresponding controlling field.

                                           Example

Contoller --

public with sharing class CreateDependentPicklist 
{
    public String selectedtState {get;set;} 
    public String selectedCtr {get;set;}
    
    public List<SelectOption> countries {get;set;}
    public List<SelectOption> states {get;set;}
    
    public CreateDependentPicklist()
    {
        countries = new List<SelectOption>();
        countries.add(new SelectOption(' ','--None--'));
        countries.add(new SelectOption('India','India'));
        countries.add(new SelectOption('America','USA'));
        countries.add(new SelectOption('China','China'));
        countries.add(new SelectOption('Italy','Italy'));
    }
    
    public void statePickList()
    {
         states = new List<SelectOption>();
        if(selectedCtr=='India')
        {
            
            states.add(new SelectOption('Jammu','Jammu'));
            states.add(new SelectOption('Rajasthan','Rajasthan'));
            states.add(new SelectOption('MadhyaPradesh','MadhyaPradesh'));
            states.add(new SelectOption('Goa','Goa'));
            states.add(new SelectOption('Kerla','Kerla'));
        }
        else if(selectedCtr=='America')
        {
            states.add(new SelectOption('New York','New York'));
            states.add(new SelectOption('Wasington DC','Washington DC'));
            states.add(new SelectOption('Seattle','Seattle'));
        }
        else
        {
            states.add(new selectOption(' ','--None--'));
        }
    }

}

Visualforce Page--

<apex:page controller="CreateDependentPicklist">
  <apex:form >
      
      <apex:pageBlock >
          <apex:pageBlockSection title="CountryList">
          
              <apex:outputLabel value="Select Country"/>             
                  <apex:selectList value="{!selectedCtr}" size="1"> 
                     <apex:selectOptions value="{!countries}"/>                        
                     <apex:actionSupport event="onchange" action="{!statePicklist}" reRender="lab"/>  
                  </apex:selectList>
                 
          </apex:pageBlockSection>
          
          <apex:pageBlockSection title="Dependent State List" id="lab">
              <apex:outputLabel value="Select State"/>
              <apex:selectList value="{!selectedtState}" Size="1">
                  <apex:selectOptions value="{!states}"/>
              </apex:selectList>
         </apex:pageBlockSection>
         
      </apex:pageblock>
      
  </apex:form>

</apex:page>


Output--



Comments

Popular posts from this blog

Dynamically Add/Delete rows in Visualforce Page

Process Automation Specialist Superbadge

How to show or hide a particular section of Visualforce Page dependent upon picklistfield value