Using Loops with Arrays and Sets
The vdl-repeat attribute
Informally introduced in previous sections, the vdl-repeat attribute is placed on a VDL or HTML element to repeat that same element by iterating over a collection.
<h1 vdl-repeat="=s in scenarios">This scenario is called <span vdl-text="=s.name"></span> </h1>The correct syntax is =<variable> in <collection> where <variable> is a symbol of your choice that can be referenced by further VDL expressions, and <collection> is an array or other JavaScript collection.
The vdl-repeat attribute repeats the element it is attached to and any child content of the element. This is in contrast to the vdl-repeat-contents attribute, described next.
The vdl-repeat-contents attribute
<h1 vdl-repeat-contents="=num in [1,2,3]"> <b vdl-text="=num"></b> </h1>In this case, using a plain JavaScript array, the output markup would resemble:
<h1> <b>1></b> <b>2</b> <b>3</b> </h1>This has repeated the <b> element and its VDL-generated content obtained by iterating over the array using a local variable called num.
This technique is useful where you might wish to repeat several groups of elements without repeating the parent element, such as a pair of table cells for each iteration.
The Loop Index
<h2 vdl-repeat="=num,idx in [10,20,30]"> <p vdl-text="='num is: ' + num + ', idx is: ' + idx"></p> </h2>Loop indexes start at zero for the first item.
HTML ids in Loops
You should avoid using the HTML id attribute on any element within a loop. HTML ids should be unique within a page, whereas adding them to elements within a loop will cause the id to be duplicated.
Instead, try using an HTML class and use CSS selectors to target elements of that class - including using nth-child() selectors if necessary. The same advice applies to jQuery selectors if you are locating elements within a loop.
Nested Loops
Both vdl-repeat and vdl-repeat-contents can be nested multiple times. Just use care to ensure that unique variable names are used in the expression for the current collection item and the index variable if used.
© 2001-2019 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.