+ Start a Discussion
RajanJasujaRajanJasuja 

Problem creating dependent picklist in Visual Force

Hi,

 

I am trying to create a simple dependent picklist in Visual force. I am using apex: actionsupport tag to re-render the dependent picklist and select the specific values for it on the basis of selected value from main picklist.

 

Mani Picklist:  Project Office

Dependent Picklit : Building  

 

The issue I am facing is when I change the value of Main picklist (Project Office) it is re-rending the other picklist. But in getter of the child picklist( getTestBuildinglist() )  I am not getting the new selected value. So in getTestBuildinglist()  function the value of SelectProjectOffices is always ‘Select One’ . Idealy it should be the selected vale(‘ABC’ or ‘XYZ’ whatever user select from the Main picklist)

 

Any idea what I am missing or any other approach which can help me to implement this.

 

Here is the sample code

 

*************************VF PAGE*******************************

<apex:page controller="testController_Rajan" cache="false" id="myPage" showHeader="false" sidebar="false" standardStylesheets="true">  
 <apex:form id="myform">
 <apex:pageBlock id="mypageBlock">
 <!-- Creating Project Office picklist and binded with SelectProjectOffice contoller -->
 <apex:pageblockSection ><apex:pageBlockSectionItem >
<apex:outputLabel value="Project Office" for="ddProjectOffice"></apex:outputLabel>
<apex:selectList value="{!selectProjectOffices}" id="ddProjectOffice" size="1" required="true">
                        <apex:actionSupport event="onchange"  reRender="ddbuilding" immediate="true" />
                        <apex:selectOptions value="{!TestProjectOfficeslist}"/>
</apex:selectList>
</apex:pageBlockSectionItem>


<apex:pageBlockSectionItem >
                    <apex:outputLabel value="Building" for="ddbuilding"></apex:outputLabel>
                    <apex:selectList value="{!Buildingvalue}" disabled="{!EnabledBuilding}"  id="ddbuilding" size="1" required="true">
                    <apex:selectOptions value="{!TestBuildinglist}"/>
                    </apex:selectList>
</apex:pageBlockSectionItem>    
</apex:pageblockSection>

</apex:pageBlock>
</apex:form>  
</apex:page>

 

**************************APEX CLASS******************************************

 

public class testController_Rajan {

    public string Buildingvalue{ get; set; }
    public string selectBuilding;
    public String SelectProjectOffices;
    public List<SelectOption> optionBuliding=new List<SelectOption>();
    public boolean EnabledBuilding{get; set;}
    List<SelectOption> options = new List<SelectOption>();

 
            /*Getting values of Status picklist  of Case Object*/
            public List<SelectOption> getTestProjectOfficeslist()
            {
              
               if(SelectProjectOffices==null)
               {
               options.add(new SelectOption('Select One','Select One'));//set default value
               options.add(new SelectOption('ABC','ABC'));//set default value
               options.add(new SelectOption('XYZ','XYZ'));//set default value
               
               /*
               for(ProjectOfficeCusList__c poff : ProjectOfficeCusList__c.getAll().values())  
               options.add(new SelectOption(poff.name,poff.name));
               */
               
               SelectProjectOffices='Select One';
               setEnabledBuilding(true);
               }
               return options;
               
           }
           
           
          public List<SelectOption> getTestBuildinglist()           
          {
           
                   
               System.Debug(getSelectProjectOffices()); // Here is the issue. It is not providing the selected Value  
               optionBuliding.add(new SelectOption(SelectProjectOffices,SelectProjectOffices));//set default value
               
               /*
               if(SelectProjectOffices!='Select One')
               {
               for(Product2 p :[select p.Building__c  from Product2 p where p.Project_Office__c=:SelectProjectOffices])
               optionBuliding.add(new SelectOption(p.Building__c, p.Building__c));
               }
               */
               setEnabledBuilding(false);
               return optionBuliding;
          
           }
           
            public String getSelectProjectOffices() {
            return this.SelectProjectOffices;
            }
            
            public void setSelectProjectOffices(String S) {
                this.SelectProjectOffices= S;
            }
            
            public Void setEnabledBuilding(Boolean b) {
                this.EnabledBuilding=b;
            }
 
 }

 

***********************************************************************************

 

Any help is appreciated.

 

Regards,

Rajan

Best Answer chosen by Admin (Salesforce Developers) 
SantoshiSantoshi
Hi, I have see your code, you are using . Remove the immediate attribute from action support. Your code will run fine on removal of this attribute.

All Answers

SantoshiSantoshi
Hi, I have see your code, you are using . Remove the immediate attribute from action support. Your code will run fine on removal of this attribute.
This was selected as the best answer
RajanJasujaRajanJasuja

 Santoshi, you are absolutely correct. I have removed and its working perfect. Thank you.

 

Cheers,

Rajan Jasuja