You need to sign in to do that
Don't have an account?

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.
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">
{!State}<br/>
<apex:repeat value="{!countryMap[country][state]}" var="city">
{!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
<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>
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.
I used your example, i could progress but not exactly what i need.
I modify code like
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
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">
{!State}<br/>
<apex:repeat value="{!countryMap[country][state]}" var="city">
{!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.