Friday, 10 January 2014

custom dependent picklist in visualforce

VF page CODE :-
<apex:page controller="custcon" tabStyle="Account" >
    <apex:form >


<apex:selectList required="true" multiselect="false" size="1" label="Type"  value="{!selectedValue}" >
<apex:selectOptions value="{!P1}"/>
<apex:actionSupport event="onchange" reRender="a"/>
</apex:selectList>


<apex:selectList required="true" multiselect="false" size="1" label="Type"   id="a">
<apex:selectOptions value="{!P2}"/>

</apex:selectList>      
       
      
       
    </apex:form>
</apex:page>
--------------------------------controller code
public with sharing class custcon {

public String selectedValue{get;set;}

public String selectedP2{get;set;}
public Boolean ShowP2;

    public custcon(){
       System.debug('~~~~~~~~~~~~selectedValue in controller~~~~~~~~~~~~~~~~ '+selectedValue);
    }
   
//get first picklist value
public List<SelectOption> getP1(){
System.debug('~~~~~~~~~~~~selectedValue in getP1~~~~~~~~~~~~~~~~ '+selectedValue);
List<SelectOption> options = new List<SelectOption>();

options.add( new SelectOption('None','--None--'));
options.add(new SelectOption('A','A'));
options.add( new SelectOption('B','B'));
return options;


}

public void setP1(String s){
    this.selectedValue = s;
    System.debug('~~~~~~~~~~~~setP1~~~~~~~~~~~~~~~~ '+s);
     System.debug('~~~~~~~~~~~~selectedValue in setP1~~~~~~~~~~~~~~~~ '+selectedValue);
   // this.ShowP2= true;
}

//Child Picklist Value

public List<SelectOption> getP2(){
System.debug('~~~~~~~~~~~~GETP2~~~~~~~~~~~~~~~~ '+selectedValue);
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('None','--None--'));

    if(selectedValue == 'A') {
    options.add(new SelectOption('aa','aa'));
    options.add(new SelectOption('aaa','aaa'));
    }
else
    if(selectedValue == 'B'){
    options.add(new SelectOption('bb','bb'));
    options.add(new SelectOption('bbb','bbb'));
    }
return options;

}
public void setP2(String s){
    this.selectedP2 =s;
   // this.ShowP2= true;
}

}