Switching between day sections in InfoPath using xdExtensions and JScript

Applies to: InfoPath 2003

Use JScript to determine the day of the week (Monday through Sunday) based on a specified date chosen from a Date Picker, and use the result to conditionally show/hide sections on an InfoPath form.

ADVERTISEMENTS

Problem

You have a TODO list in InfoPath for each working day of the week (similar to a daily agenda) and would like to be able to display the list for a day of the week (Monday, Tuesday, Wednesday, Thursday, or Friday) which is determined by a chosen date.

Solution

Use an xdExtension function written in JScript to determine which day of the week pertains to the selected date and use Conditional Formatting in InfoPath to show/hide the section that belongs to a particular day of the week.

Discussion

You can accomplish this functionality as follows:

  1. Create a New Blank Form in InfoPath.
  2. Add a Table with Title to the InfoPath form.
  3. Add a Date Picker control to the table and rename it to date.
  4. Add 5 Section controls below the Date Picker control.

  5. Your Main data source should now resemble the following figure:
    Main data source of the InfoPath form
    Figure 1. Main data source of the InfoPath form.
  6. Type the text Monday, Tuesday, Wednesday, Thursday, and Friday on each subsequent Section control.
  7. Modify the table and controls to resemble the following figure:
    The InfoPath form in design mode
    Figure 2. The InfoPath form in design mode.
  8. Go to Tools > Programming > Microsoft Script Editor to open Microsoft Script Editor and add the following code at the end of the script.js file:
    function isDay(dayNo)
    {
    var retVal = false;

    var isoDate = XDocument.DOM.selectSingleNode("//my:date").text;
    var year = isoDate.substr(0,4);
    var month = isoDate.substr(5,2);
    var day = isoDate.substr(8,2);
    var date = new Date(parseFloat(year), parseFloat(month) - 1, parseFloat(day));

    if (dayNo == date.getDay())
    {
    retVal = true;
    }

    return retVal;
    }
  9. Double-click on the Section control for Monday to open its properties dialog box.
  10. Click on the Display tab and then on the Conditional Formatting... button.
  11. Click on the Add... button on the Conditional Formatting dialog box.
  12. Select The expression from the first drop-down list box and then type in the following expression:
    xdExtension:isDay(1) = false()
  13. Under Then apply this formatting: check the Hide this control checkbox.
  14. Click on OK when closing all open dialog boxes.
  15. Repeat steps 8 through 13 for all the other Section controls, using the following expressions:
    xdExtension:isDay(2) = false()
    for the Tuesday section,
    xdExtension:isDay(3) = false()
    for the Wednesday section,
    xdExtension:isDay(4) = false()
    for the Thursday section, and
    xdExtension:isDay(5) = false()
    for the Friday section.
  16. Double-click on the Date Picker control to open its properties dialog box.
  17. Click on the Insert Formula button behind the Default Value text box.
  18. Click on the Insert Function... button on the Insert Formula dialog box and select today from the list of functions.
  19. Click on OK when closing all open dialog boxes.

You should now have fully functional day of the week section selection based on a specified date.

 


Related InfoPath Articles:

 

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. 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.

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