Displaying Dependent Picklist Fields on a Visualforce Page

Dependent picklists in Salesforce are very useful.  They are used to limit the list of values in one picklist based on the value of another picklist.  An example would be if you had a list of countries, states/provinces and major cities.  If you select a country you would want the list of states/provinces to be filtered in the next picklist.  The state/province selected should then limit the major cities that would be available for selection.

Displaying dependent picklists is very simple with a Visualforce pageblockTable. Let’s assume that we have a custom object called locations with three custom fields called country__c, state__c and city__c.  When the custom object is viewed the ‘Controlling Field’ column for state will be country and for city it will be state.  If you go into the custom field for country or state you will see a ‘Field Dependencies’ section that will show the custom field that is being controlled by the values in this picklist. If the ‘edit’ link is clicked the dependent values can be managed by selecting values and click on the ‘Include Values’ or ‘Exclude Values’ buttons. Now that our dependent picklists are setup we can create our table by simply referencing those fields in the pageblockTable tag.

Controller First…

1
2
3
4
5
6
7
8
9
10
11
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];
    }
    
}
</location__c>

Visualforce Page Second…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<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>

When this table renders it looks like this:

Location Table

Now what if there is a requirement that the country field should not be displayed?  But the requirement is still that the state picklist should only display the correct values for that country.  With some trial and error I found out that the country picklist has to be on the page.  I tried to set the render property of the column to be false, but that did not work.  I also tried to set the width of the column to be 1%, but it seemed like a minimum length was going to be given to display the field no matter how I set the length.  Finally I ran across some css that would do the trick.  Set the style value for the column to be this:  style=“display:none;visibility:hidden”.  Then the column is really there, but just not visible to the end user.  The full column in Visualforce would look like this:

1
2
3
4
<apex:column headervalue="Country" style="display:none;visibility:hidden">
     <apex:inputfield value="{!locItem.Country__c}">
     </apex:inputfield>
</apex:column>

Dependent picklist fields are very powerful and integrate so easily with Visualforce, but sometimes we need a little css help for a few final requirements.

Happy Visualforcing!

This entry was posted in Technology and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>