200 IBM Workplace Forms: Guide to Building and Integrating a Sample Workplace Forms Application
For loops and nesting loops in XFDL
The For function creates a counter that you can use to simulate a “for loop” (as found in most
programming languages).
This function uses an option in your form (such as a custom option) as an index that stores
the current count of the loop. As the loop counts, it sets this option to the current count.
For example, if the for loop counted from 1 to 3, it would first set the option to 1, then to 2,
then to 3.
You can then trigger other action in the form based on the value of this option. For example,
you could use the toggle function to detect each change in the option's value, and to update
some aspect of the form each time.
A form with a for loop will begin counting that loop as soon as the form opens, unless the loop
itself relies on a triggering event, such as a key-press event or a toggle function.
You can create a loop that counts once by setting the initial count and the final count to be
equal. For example, a loop that counts from 1 to 1 will count once.
To create a nested loop, you must trigger the second loop off the value of the first. For
example, your first loop might change the value of the custom:loop1 option. You can then
create a toggle function that detects any change in the custom:loop1 option, and that triggers
its own loop, as shown in Example 5-30.
Example 5-30 Nesting Loops in XFDL
toggle(custom:loop1) == '1' ? for('custom:loop2', '1', '5') : ''
The second loop is triggered each time the value of custom:loop1 changes. In other words,
the second loop is triggered each time the first loop counts once. Additionally, the second loop
will process completely before processing of the first loop resumes. In this case, that means
the second loop will count from 1-5 before the first loop counts again.
Example 5-31 shows the custom computes used in the customer selection pop-up to
prepopulate the choices by consuming the Customer Data Web service described in 5.3,
“Web services” on page 168.
Example 5-31 Nested Loop to prepopulate Customer pop-up using a Web service
<custom:getCustomerChoices xfdl:compute="
(toggle(custom:CustomerChoicesCounter) == '1' and (custom:CustomerChoicesCounter == '0'))
or (CustomerChoices_1.value=='')
? set('custom:CustomerChoicesList', CustomerInfoService_WPFormsCust.GETCUSTOMERLIST('') +. '~')
+ for('custom:CustomerChoicesCounter', '1', strlen(custom:CustomerChoicesList))
: ''">
</custom:getCustomerChoices>
<custom:getCustomerChoices2 xfdl:compute="
Tip: When creating the option that acts as an index for the loop, ensure that the starting
value of the option does not equal the starting value of the loop. If it does, your loop will
work incorrectly, since setting the first count will not result in a detectable change.
For example, you might create a loop that counts from 1-5, and your index might be set to
a value of 1. In this case, the first count of the loop will set the index to 1, but since the
index already equals 1, there will be no detectable change to the value. This means that
any toggle function used to detect a change in that value will not fire.