Create a shrinking list in a repeating table in InfoPath

Learn how to use rules and conditions in InfoPath to create a drop-down list box that shrinks in items as rows are inserted into the repeating table that contains the drop-down list box.

ADVERTISEMENTS

Problem

You have a repeating table in InfoPath, which contains a drop-down list box in a column in each one of its rows. Whenever you add a new row to the repeating table you want the drop-down list box in the newly added row to contain items which have not been selected in any of the drop-down list boxes in previous rows of the repeating table. Less items should be present in the drop-down list box with each row you insert.

Solution

Use Rules and Conditions in InfoPath to create a drop-down list box that shrinks in the amount of items it contains when rows are inserted in the repeating table that contains the drop-down list box.

Discussion

You have the following XML file that contains the names of players, 9 of whom you would like to line up for a match.

<?xml version="1.0" encoding="utf-8" ?>
<players>
<player name="Brian McCann" no="16" pos="C" />
<player name="Brayan Pena" no="8" pos="C" />
<player name="Todd Pratt" no="--" pos="C" />
<player name="John Smoltz" no="29" pos="P" />
<player name="Jorge Sosa" no="34" pos="P" />
<player name="Lance Cormier" no="--" pos="P" />
<player name="Adam LaRoche" no="19" pos="1B" />
<player name="Pete Orr" no="4" pos="2B" />
<player name="Marcus Giles" no="22" pos="2B" />
<player name="Chipper Jones" no="10" pos="3B" />
<player name="Wilson Betemit" no="24" pos="3B" />
<player name="Edgar Renteria" no="--" pos="SS" />
<player name="Kelly Johnson" no="27" pos="LF" />
<player name="Andruw Jones" no="25" pos="CF" />
<player name="Ryan Langerhaus" no="18" pos="RF" />
<player name="Jeff Francoeur" no="7" pos="RF" />
</players>

A baseball team line-up consists of 9 players with each player filling one of the 9 field positions: C=Catcher, P=Pitcher, 1B=First Base, 2B=Second Base, 3B=Third Base, SS=Short Stop, LF=Left Field, CF=Center Field, RF=Right Field.

So you would like to:

  • List 9 players, each with their name, number, and position on the field.
  • Select a player from a list of players; a player may only be selected once.
  • Automatically display the number and position of each player.

Create a new InfoPath form and add a layout table and repeating table with 3 columns to the form template. Change the first field into a drop-down list box and remove the borders and shading from the second and third fields.

The InfoPath form template in design mode. The form contains a repeating table with a drop-down list box in the first column of the repeating table.

Rename the nodes in the Main data source to resemble the following image.

The Main data source of the InfoPath form template.

Add a secondary data source to the InfoPath form by adding a data connection to the players XML file. The secondary data source should resemble the following image.

The secondary data source bound to the XML file.

Now you have everything set up to implement the solution.

Populate the drop-down list box with unique players

  1. Double-click on the drop-down list box to open its Properties dialog box.
  2. Select the connection for the XML file in the Data Connection drop-down list box.
  3. Click on the Select XPath button behind the Entries field.
  4. Select the player node in the Select a Field or Group dialog box.
  5. Click on the Filter Data... button.
  6. Click on the Add... button in the Filter Data dialog box.
  7. Select name in the first drop-down list box in the Specify Filter Conditions dialog box.
  8. Select Select a field or group... in the third drop-down list box.
  9. Change the data source to Main in the Data Source drop-down list box.
  10. Expand the tree, select the name node, and click on the OK button.
  11. Back in the Specify Filter Conditions dialog box, select The expression in the first drop-down list box. The expression should look like the following:
    @name = xdXDocument:get-DOM()/my:myFields/my:players/my:player/my:name
  12. Change the expression in the expression textbox to:
    not(@name = xdXDocument:get-DOM()/my:myFields/my:players/my:player/my:name)
    This expression will exclude all players from the drop-down list box whose names have already appeared in another row of the table.
  13. Click on the And >> button.
  14. Repeat steps 7 through 12 but then for the @pos node and position field. This second expression will exclude all players from the drop-down list box whose position have already been taken in another row of the table. The second expression should look like:
    not(@pos = xdXDocument:get-DOM()/my:myFields/my:players/my:player/my:position)
  15. Click on the OK button on all open dialog boxes except for the Drop-Down List Box Properties dialog box to save the changes and close them.
  16. Click on the Select XPath button behind the Value field.
  17. Select the name node and click on the OK button.
  18. Click on the Select XPath button behind the Display name field.
  19. Select the name node and click on the OK button.
  20. Click on the OK button in the Drop-Down List Box Properties dialog box.

Within each row, the drop-down list box should now contain only the names of those players who have not already been selected in any other row and whose field position has not already been taken by another player in any other row. After the 9th player has been selected, the drop-down list box should be empty.

As rows are inserted into the repeating table, the drop-down list box contains less and less items. Once 9 rows have been added, the drop-down list box will be empty.

Automatically display the player's number and position

The current() function returns the currently selected row in a repeating group. name is the field in the Main data source that stores the value of the drop-down list box on our InfoPath form. When the current() function is used in filters on the name field, it returns the value of the name field for the currently selected row of the repeating table.

You will use this behavior of the current() function to display the correct player's number and position corresponding to the player's name selected in the drop-down list box in the currently selected row. For this you will add two rules on the name field to set the values of the number and position fields.

To add a rule to display the player's number:

  1. Double-click on the drop-down list box to open its Properties dialog box.
  2. Click on the Rules... button under the Validation and Rules section.
  3. Click on the Add... button in the Rules dialog box.
  4. Click on the Add Action... button in the Rule dialog box.
  5. Select Set a field's value in the Action drop-down list box.
  6. Click on the Select a Field or Group button behind the Field text box.
  7. Click on the number node in the Select a Field or Group dialog box and click on the OK button.
  8. Click on the Inser Formula button behind the Value text box.
  9. Click on the Insert Field or Group... button in the Insert Formula dialog box.
  10. Select the secondary data source for the players XML file in the Data Source drop-down list box in the Select a Field or Group dialog box.
  11. Click on the no node and then on the Filter Data... button.
  12. Click on the Add... button in the Filter Data dialog box.
  13. Select name in the first drop-down list box on the Specify Filter Conditions dialog box.
    The Specify Filter Conditions dialog box in InfoPath
  14. Select The expression in the first drop-down list box on the Specify Filter Conditions dialog box to display the XPath expression behind the filter condition you just specified.
    The Specify Filter Conditions dialog box in InfoPath once The expression has been selected from the first drop-down list box.
  15. Replace the "" in the expression text box with current(). The final expression should look like:
    ../@name = current()
  16. Click on the OK button on all open dialog boxes to close them.

Follow the same steps as when adding a rule to display the player's number to add a rule to display the player's position, but select the fields in the Main and Secondary data sources that correspond to the position field instead of the number field:

  • position node in the Main data source
  • pos node in the Secondary data source

The second rule should resemble the following image.

The Rule dialog box in InfoPath

Our form should now automatically display the number and position whenever a player is selected from the drop-down list box.

 
 Subscribe for updates via RSS or Email

Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent of S.Y.M. Wong-A-Ton. The techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author, S.Y.M. Wong-A-Ton.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners
InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013
InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners
InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010
InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers
InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services


Related InfoPath Articles:

ADVERTISEMENTS

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services