Create a Cascading List from a Flat Data Source - Greg Collins
in

InfoPath Dev

This Blog

Syndication

Greg Collins

Create a Cascading List from a Flat Data Source

It is common to need two or more list boxes where the choices in one are dependant on the value of another. This is known as a cascading list. A cascade can occur with as few as two levels up to any number of levels. Some common scenarios which require cascading lists are:

  • Location: State / City
  • Vehicle: Make / Model / Trim
  • School: Year / Semester / Class / Instructor

As the number of levels in the cascade increases, so does the complexity of the process and work involved. Also, if the cascading list is presented inside of a repeating table or repeating section you must account for another issue: data crossover. Repeating cascading lists are covered in Create a Repeating Cascading List from a Flat Data Source and Create a Repeating Cascading List from a Hierarchical Data Source.

In this task we will create a flat secondary data source containing the values used to populate our lists. We will then create a three-level non-repeating cascading list using drop-down list boxes. We create three levels of cascade to reveal the compounding complexity involved for level three and beyond. Because of the nature of the flat data source, we will filter each list to ensure that only unique values are displayed. And finally, we ensure valid selection combinations by clearing selected list values as selections earlier in the cascade are changed.


THE FLAT SECONDARY DATA SOURCE

Flat data sources are commonly returned from databases, Web services, and SharePoint. A flat data source has no hierarchy in the structure; it is simply a list of identical elements, each with an identical set of attributes. Only the values of the attributes change.  Flat data sources also require additional filtering to ensure that only unique values appear in the drop-down lists. Let's start by creating the flat secondary data source.

Create the flat secondary data source:

Copy the following code into a text editor, and then save the file as Vehicles - Flat Data Source.xml. This file will be used as a secondary data source in our form.

<?xml version="1.0" encoding="UTF-8"?>
<Vehicles>
    <Vehicle year="1963" make="Chevrolet" model="Corvette"/>
    <Vehicle year="1963" make="Chevrolet" model="Nova"/>
    <Vehicle year="1963" make="Porsche" model="356"/>
    <Vehicle year="1963" make="Porsche" model="Carrera"/>
    <Vehicle year="1973" make="Ford" model="Bronco"/>
    <Vehicle year="1973" make="Ford" model="Mustang"/>
    <Vehicle year="1973" make="Ford" model="Pinto"/>
    <Vehicle year="1973" make="Toyota" model="Celica"/>
    <Vehicle year="1973" make="Toyota" model="Land Cruiser"/>
    <Vehicle year="1979" make="Jaguar" model="XJ6"/>
    <Vehicle year="1979" make="Pontiac" model="Trans Am"/>
    <Vehicle year="1979" make="Volvo" model="240"/>
    <Vehicle year="1979" make="Volvo" model="244DL"/>
    <Vehicle year="1987" make="Dodge" model="Ram Charger"/>
    <Vehicle year="1987" make="Dodge" model="Grand Caravan"/>
    <Vehicle year="1987" make="Honda" model="Civic"/>
    <Vehicle year="1987" make="Honda" model="Prelude"/>
    <Vehicle year="1987" make="Nissan" model="300ZX"/>
    <Vehicle year="1987" make="Nissan" model="Pulsar"/>
    <Vehicle year="1987" make="Nissan" model="Sentra"/>
    <Vehicle year="1999" make="GMC" model="Jimmy"/>
    <Vehicle year="1999" make="GMC" model="Sierra 2500 HD"/>
    <Vehicle year="1999" make="GMC" model="Yukon"/>
    <Vehicle year="1999" make="Honda" model="Passport"/>
    <Vehicle year="1999" make="Honda" model="Prelude"/>
    <Vehicle year="1999" make="Honda" model="CR-V"/>
    <Vehicle year="1999" make="Jeep" model="Cherokee"/>
    <Vehicle year="1999" make="Mercury" model="Grand Marquis"/>
    <Vehicle year="1999" make="Mercury" model="Sable"/>
    <Vehicle year="1999" make="Mercury" model="Villager"/>
    <Vehicle year="1999" make="Pontiac" model="Firebird"/>
    <Vehicle year="1999" make="Pontiac" model="Montana"/>
    <Vehicle year="1999" make="Pontiac" model="Sunfire"/>
    <Vehicle year="2003" make="Dodge" model="Dakota"/>
    <Vehicle year="2003" make="Dodge" model="Caravan"/>
    <Vehicle year="2003" make="Dodge" model="Ram Charger"/>
    <Vehicle year="2003" make="Dodge" model="Viper"/>
    <Vehicle year="2003" make="Mercury" model="Sable"/>
    <Vehicle year="2004" make="Dodge" model="Stratus"/>
    <Vehicle year="2004" make="Dodge" model="Viper"/>
    <Vehicle year="2004" make="Ford" model="Crown Victoria"/>
    <Vehicle year="2004" make="Ford" model="F250"/>
    <Vehicle year="2004" make="Ford" model="Thunderbird"/>
    <Vehicle year="2004" make="GMC" model="Sierra 3500"/>
    <Vehicle year="2004" make="Jeep" model="Grand Cherokee"/>
    <Vehicle year="2004" make="Jeep" model="Liberty"/>
</Vehicles>


THE FORM

Now that we have our flat secondary data source we are ready to create our form. First we will design a simple schema to support a three-level cascading list. Then we will design the layout using drop-down list boxes. When the layout is complete we will focus on creating the cascade.

Add the secondary data source to a form:

  1. Design a new blank form.
  2. Choose Data Connections from the Tools menu, and then click Add.
  3. In the Data Connection Wizard, select Receive Data, and then click Next.
  4. Select XML Document, and then click Next.
  5. Click Browse, locate and select the Vehicles - Flat Data Source.xml file, click Open, and then click Next.
  6. Click Finish, click Yes, and then click Close.

Create the schema:

  1. Open the Data Source task pane.
  2. Add a Group named Vehicle.
  3. Select the Vehicle group, and then add three Text Fields named Year, Make, and Model.

Design the view:

  1. Insert a 1x3 layout table into the view.
  2. From left to right, insert the Year, Make, and Model fields as Drop-Down List Boxes into the cells of the layout table, as shown in Figure 1.


Figure 1. Layout of the cascading drop-down list boxes.


THE CASCADING LISTS

With the form layout complete we will now turn our attention to the meat of the task: making one list cascade off of another. The process is not very difficult, but can become increasingly complex as the number of cascade levels increases. Creating a cascade requires the use of filters. The options displayed in one list box are filtered based on the values selected in earlier list boxes. Let's populate each drop-down list box using our secondary data source and create the cascade filters.

Populate the Year drop-down list:

  1. Double-click the Year drop-down list box.
  2. In the Drop-Down List Box Properties dialog box select Look Up Values In A Data Connection.
  3. Select Vehicles - Flat Data Source from the Data Connection drop-down list.
  4. Click the Select XPath button to the right of the Entries text box, select Vehicles/Vehicle/year, and then click OK twice to close the open dialog boxes.

Populate the Make drop-down list:

  1. Double-click the Make drop-down list box.
  2. In the Drop-Down List Box Properties dialog box select Look Up Values In A Data Connection.
  3. Select Vehicles - Flat Data Source from the Data Connection drop-down list.
  4. Click the Select XPath button to the right of the Entries text box, select Vehicles/Vehicle/make.

Filter the Make drop-down list based on the selected Year:

  1. Click Filter Data, and then click Add.
  2. In the Specify Filter Conditions dialog box, select Select A Field Or Group from the first drop-down list.
  3. In the Select A Field Or Group dialog box, select Vehicles/Vehicle/year, and then click OK.
  4. In the second drop-down list, select Is Equal To.
  5. In the third drop-down list box, select Select A Field Or Group.
  6. In the Select A Field Or Group dialog box, select Main from the Data Source drop-down list, select myFields/Vehicle/Year, and then click OK (refer to Figure 2).


Figure 2. Filtering the second level of a cascading list.

  1. Click OK four times to close all open dialog boxes.

At this point you have created a two-level cascade. The options in the Make drop-down list are filtered based on the selected Year. You can test this by previewing your form. Now let's create the level-three cascade, which will reveal the compounding complexity involved in creating each additional level of cascade from here.

Populate the Model drop-down list:

  1. Double-click the Model drop-down list box.
  2. In the Drop-Down List Box Properties dialog box select Look Up Values In A Data Connection.
  3. Select Vehicles - Flat Data Source from the Data Connection drop-down list.
  4. Click the Select XPath button to the right of the Entries text box, and then select Vehicles/Vehicle/model.

Filter the Model drop-down list based on the selected Year:

  1. Click Filter Data, and then click Add.
  2. In the Specify Filter Conditions dialog box, select Select A Field Or Group from the first drop-down list.
  3. In the Select A Field Or Group dialog box, select Vehicles/Vehicle/year, and then click OK.
  4. In the second drop-down list, select Is Equal To.
  5. In the third drop-down list box, select Select A Field Or Group.
  6. In the Select A Field Or Group dialog box, select Main from the Data Source drop-down list, select myFields/Vehicle/Year, and then click OK.

Filter the Model drop-down list based on the selected Make:

  1. Click And to create a second filter condition.
  2. In the first drop-down list of the second filter condition, select Select A Field Or Group.
  3. In the Select A Field Or Group dialog box, select Vehicles/Vehicle/make, and then click OK.
  4. In the second drop-down list, select Is Equal To.
  5. In the third drop-down list box, select Select A Field Or Group.
  6. In the Select A Field Or Group dialog box, select Main from the Data Source drop-down list, select myFields/Vehicle/Make, and then click OK (refer to Figure 3).


Figure 3. Filtering the third level of a cascading list.

  1. Click OK four times to close all open dialog boxes.

You have now created a three-level cascade. Preview the form and test the functionality of the drop-down list boxes. You may discover two issues: first, the list options are not unique, and second, you can create invalid selection combinations. These two issues will be resolved in the next two sections.


FILTERING LISTS FOR UNIQUE VALUES

Values in flat data sources are usually repeated. Although this is important given the XML structure, it can lead to option bloat in your list box. It can only serve to confuse and frustrate the user to present them with numerous identical options. The answer is to filter the list to display only unique values. This can be done using XPath.

It should be mentioned that just as the complexity of the cascading filters compounded with each additional cascading level, so does the complexity of the uniqueness filters. You will see this in the expression used to filter the Model drop-down list for unique values.

Add a uniqueness filter to the Year drop-down list:

  1. Double-click the Year drop-down list box.
  2. In the Drop-Down List Box Properties dialog box, click the Select XPath button to the right of the Entries text box.
  3. In the Select A Field Or Group dialog box, click Filter Data, and then click Add.
  4. In the Specify Filter Conditions dialog box, select The Expression from the first drop-down list, and then type the following expression (as shown in Figure 4):

not(. = ../preceding-sibling::Vehicle/@year)


Figure 4. Filtering list options to display only unique values.

  1. Click OK four times to close all open dialog boxes.

Add a uniqueness filter to the Make drop-down list:

At each cascade level beyond the initial list box, your uniqueness filter must take into consideration the selection from each of the previous levels. Due to the nature of this filter, if you do not take the previous selections into consideration, you may inadvertently completely filter out all valid options.

For example, in our sample secondary data source, vehicle years 1987 and 1999 both include the Honda make. If we did not account for the selected year, the Honda make would not appear as an option when year 1999 was selected. This is because the filter would identify that the Honda make appeared earlier in the secondary data source. The Dodge make in years 2003 and 2004 would also be completely filtered out because Dodge appears earlier in year 1987.  There are a few other makes and models in the sample secondary data source that would encounter this issue.

  1. Double-click the Make drop-down list box.
  2. In the Drop-Down List Box Properties dialog box, click the Select XPath button to the right of the Entries text box.
  3. In the Select A Field Or Group dialog box, click Filter Data, and then click Add.
  4. In the Specify Filter Conditions dialog box, select The Expression from the first drop-down list, and then type the following expression:

not(. = ../preceding-sibling::Vehicle[@year = current()/my:Vehicle/my:Year]/@make)

Note that compared with the uniqueness filter we placed on the Year drop-down list box, there is an additional subfilter specified on the Vehicle node. The subfilter [@year = current()/my:Vehicle/my:Year] constrains the main filter to verify that we display unique values for each make within the selected year.

  1. Click OK four times to close all open dialog boxes.

Add a uniqueness filter to the Model drop-down list:

  1. Double-click the Model drop-down list box.
  2. In the Drop-Down List Box Properties dialog box, click the Select XPath button to the right of the Entries text box.
  3. In the Select A Field Or Group dialog box, click Filter Data, and then click Add.
  4. In the Specify Filter Conditions dialog box, select The Expression from the first drop-down list, and then type the following expression:

not(. = ../preceding-sibling::Vehicle[@year = current()/my:Vehicle/my:Year and @make = current()/my:Vehicle/my:Make]/@model)

Just as we did with the uniqueness filter for the Make drop-down list above, here we have added an additional subfilter to guarantee that we are only eliminating repeated options from the list of models within the selected year and selected make.

  1. Click OK four times to close all open dialog boxes.

If you can guarantee that the values, at any level of cascade, will always and forever be unique then you do not need to add the uniqueness filter. If, however, there is any doubt, it is always safer to add it. For more information on filtering lists for unique values, refer to Display Only Unique Options in a List.


CLEARING SELECTIONS AFTER CHANGES

One issue with cascading lists that needs to be addressed is that when a selection is changed in an earlier level the selections following that change are likely to be invalid. For example, if you select year 1963 and model Chevrolet, and then select year 1973, Chevrolet remains the selected model, even though our data source does not list it as a model for 1973. The old value needs to be cleared so that invalid selection combinations cannot exist. This can be easily corrected with Rules.

With some extra work, you can probably determine whether the following selected values are still valid and clear only invalid ones, but there are advantages to just clearing each following selected value. One advantage is that rules can cascade as well—like a Domino effect. As one value is changed, the next value is cleared; clearing that value triggers the next value to clear, and so on.

The rules we create will have no condition so that they will be processed every time the selected value changes. To clear the selected value of a field, we leave the Value text box of the rule blank.

Clear the selected Make when the selected Year changes:

  1. Double-click the Year drop-down list box.
  2. In the Drop-Down List Box Properties dialog box, click Rules, and then click Add.
  3. Name the rule Clear Make, and then click Add Action.
  4. In the Action dialog box, select Set A Field's Value from the Action drop-down list.
  5. Click the Select XPath button to the right of the Field text box, and then select myFields/Vehicle/Make in the Main data source.
  6. Click OK five times to close all open dialog boxes.

Clear the selected Model when the selected Make changes:

  1. Double-click the Make drop-down list box.
  2. In the Drop-Down List Box Properties dialog box, click Rules, and then click Add.
  3. Name the rule Clear Model, and then click Add Action.
  4. In the Action dialog box, select Set A Field's Value from the Action drop-down list.
  5. Click the Select XPath button to the right of the Field text box, and then select myFields/Vehicle/Model in the Main data source.
  6. Click OK five times to close all open dialog boxes.

Try it:

You have now created a three-level cascading list that filters for unique values and prevents invalid selection combinations. Preview the form and try various selection combinations.

©2005 Greg Collins. All rights reserved. Licensed to Autonomy Systems, LLC for display on InfoPathDev.com.

Comments

 

cascading dropdownlist problem | keyongtech said:

Pingback from  cascading dropdownlist problem | keyongtech

January 18, 2009 8:27 AM
 

Viewing Limited dropdown lists | keyongtech said:

Pingback from  Viewing Limited dropdown lists | keyongtech

January 21, 2009 9:05 PM
 

Canon printer driver - Canon bjc - Canon bjc 80 printer driver said:

Pingback from  Canon printer driver - Canon bjc - Canon bjc 80 printer driver

August 13, 2010 7:53 PM
 

Largest king cobra - Cobra irons - King cobra said:

Pingback from  Largest king cobra - Cobra irons - King cobra

September 8, 2010 3:46 AM
 

Sample graduation announcement - Announcement sample - Graduation announcements said:

Pingback from  Sample graduation announcement - Announcement sample - Graduation announcements

September 13, 2010 1:43 PM
 

Statik said:

Just wanted to say this was exactly what I was looking for and the instructions were brilliant.  I found it even easier to just copy and paste the instructions into Word and then replace Year, Make, and Model with my own. thank you!

May 11, 2012 4:38 AM
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.