function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SimrinSimrin 

Displaying picklist and dependent picklist using pageBlockSection

Hello,

I followed blog below to display two dependent picklist
https://www.sundoginteractive.com/sunblog/posts/displaying-dependent-picklist-fields-on-a-visualforce-page

public with sharing class locationController{
 
    public list<location__c> locationList{get;set;}
 
    public locationController(){
        locationList = [Select ID, Country__c, State__c, City__c
                From Location__c];
    }
     
}



<apex:page controller="locationController"> 
  Location Table
  <apex:messages style="color:red"></apex:messages>
   
    <apex:form>
        <apex:pageblock>
            <apex:pageblocktable value="{!locationList}" var="locItem">
                <apex:column headervalue="Country">
                    <apex:inputfield value="{!locItem.Country__c}">
                     </apex:inputfield>
               </apex:column>
             
              <apex:column headervalue="State/Province">
                    <apex:inputfield value="{!locItem.State__c}">
                    </apex:inputfield>
               </apex:column>
             
              <apex:column headervalue="City">
                    <apex:inputfield value="{!locItem.City__c}">
                    </apex:inputfield>
               </apex:column>
            </apex:pageblocktable>
        </apex:pageblock>
    </apex:form>
   
</apex:page>
 

How can this example be modified to use "pageBlockSection",  or is there any better way to display picklist and dependent picklist like a pageBlockSection fashion.

 

Best Answer chosen by Simrin
Swayam  AroraSwayam Arora
Below is the code you want:-

Class:-
public with sharing class LocationController{
 
    public List<location__c> locationList{get;set;}
    public Map<String, Map<String, List<String>>> countryMap{get;set;}
    
    public locationController(){
        countryMap = new Map<String, Map<String, List<String>>>();
        
        locationList = [Select ID, Country__c, State__c, City__c From Location__c];
        
        for(Location__c loc : locationList) {
            if(countryMap.containsKey(loc.Country__c)) {
                if(countryMap.get(loc.Country__c).containsKey(loc.State__c)) {
                    countryMap.get(loc.Country__c).get(loc.State__c).add(loc.City__c);
                } else {                    
                    countryMap.get(loc.Country__c).put(loc.State__c, new List<String>{loc.City__c});
                }
            } else {
                Map<String, List<String>> m = new Map<String, List<String>>();
                m.put(loc.State__c, new List<String>{loc.City__c});
                countryMap.put(loc.Country__c, m);//new Map<String, List<String>>{loc.State__c : new List<String>{loc.State__c}}
            }
        }
    }     
}

Page:-
<apex:page controller="LocationController">
  Location Table
  <apex:messages style="color:red"></apex:messages>   
    <apex:form >
        <apex:pageBlock >
            <apex:repeat value="{!countryMap}" var="country">
                {!country}<br/>
                <apex:repeat value="{!countryMap[country]}" var="state">
                    &nbsp;&nbsp;&nbsp;&nbsp;{!State}<br/>
                    <apex:repeat value="{!countryMap[country][state]}" var="city">        
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{!city}<br/>
                    </apex:repeat>
                </apex:repeat>
            </apex:repeat>
        </apex:pageBlock>
    </apex:form>   
</apex:page>

Please mark the answer as Best Answer if the code really helped so that it help others as well.

All Answers

Vishal NegandhiVishal Negandhi
Does this help your cause.

<apex:pageBlock title="Field Dependency on VF" >
        <apex:repeat value="{!locationList}" var="locItem">
            <apex:pageBlockSection columns="1">
                <apex:inputField value="{!locItem.Country__c}" />
                <apex:inputField value="{!locItem.State__c}" />
            </apex:pageBlockSection>
        </apex:repeat>
        </apex:pageBlock>
Swayam  AroraSwayam Arora
Hi Kiran,

Is this what you are looking for?

<apex:page controller="LocationController">
  Location Table
  <apex:messages style="color:red"></apex:messages>
   
    <apex:form>
        <apex:pageBlock>
        <apex:repeat value="{!locationList}" var="loc">
            <apex:pageBlockSection columns="6">
                <apex:inputField value="{!loc.Country__c}" />
                <apex:inputField value="{!loc.State__c}" />
                <apex:inputField value="{!loc.City__c}" />
            </apex:pageBlockSection>
        </apex:repeat>
        </apex:pageBlock>
    </apex:form>
   
</apex:page>


Please mark the answer as Best Answer if the code really helped so that it help others as well.
SimrinSimrin

I used your example, i could progress but not exactly what i need.
I modify code like

<apex:pageBlock title="Field Dependency on VF" >
        <apex:repeat value="{!locationList}" var="locItem">
            <apex:pageBlockSection title="{!locItem.Country__c}" columns="1">
               <apex:pageBlockSection title="{!locItem.State__c}" columns="1">
                    <apex:pageBlockSection title="{!locItem.State__c}" columns="1">
                   </apex:pageBlockSection>               
               </apex:pageBlockSection>
            </apex:pageBlockSection>
        </apex:repeat>
        </apex:pageBlock>

But it is displaying in right user interface i desire but bot correct data.

It is displaying like
+India
    +Maharashtra
        +Mumbai
+India
    +Maharashtra
        +¨Pune
+India
    +Karnataka
        +Bangalore

But what i need is

+India
    +Maharashtra
        +Mumbai
        +Pune
    +Karnataka
        +Bangalore
Swayam  AroraSwayam Arora
Below is the code you want:-

Class:-
public with sharing class LocationController{
 
    public List<location__c> locationList{get;set;}
    public Map<String, Map<String, List<String>>> countryMap{get;set;}
    
    public locationController(){
        countryMap = new Map<String, Map<String, List<String>>>();
        
        locationList = [Select ID, Country__c, State__c, City__c From Location__c];
        
        for(Location__c loc : locationList) {
            if(countryMap.containsKey(loc.Country__c)) {
                if(countryMap.get(loc.Country__c).containsKey(loc.State__c)) {
                    countryMap.get(loc.Country__c).get(loc.State__c).add(loc.City__c);
                } else {                    
                    countryMap.get(loc.Country__c).put(loc.State__c, new List<String>{loc.City__c});
                }
            } else {
                Map<String, List<String>> m = new Map<String, List<String>>();
                m.put(loc.State__c, new List<String>{loc.City__c});
                countryMap.put(loc.Country__c, m);//new Map<String, List<String>>{loc.State__c : new List<String>{loc.State__c}}
            }
        }
    }     
}

Page:-
<apex:page controller="LocationController">
  Location Table
  <apex:messages style="color:red"></apex:messages>   
    <apex:form >
        <apex:pageBlock >
            <apex:repeat value="{!countryMap}" var="country">
                {!country}<br/>
                <apex:repeat value="{!countryMap[country]}" var="state">
                    &nbsp;&nbsp;&nbsp;&nbsp;{!State}<br/>
                    <apex:repeat value="{!countryMap[country][state]}" var="city">        
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{!city}<br/>
                    </apex:repeat>
                </apex:repeat>
            </apex:repeat>
        </apex:pageBlock>
    </apex:form>   
</apex:page>

Please mark the answer as Best Answer if the code really helped so that it help others as well.
This was selected as the best answer
SimrinSimrin
I modified it using pageBlockSection  but yours is the best Solution indeed. Thank you
Swayam  AroraSwayam Arora
Welcome :)