Home   Single Page

The forEach Attribute

The forEach attribute is used to control how many components shall be created. If you specify a collection of objects to this attribute, ZK Loader will create a component for each item of the specified collection. For example, in the following ZUML page, the listitem element will evaluated three times (for "Monday", "Tuesday" and "Wednesday") and then generate three list items.

<zscript>contacts = new String[] {"Monday", "Tuesday", "Wednesday"};</zscript>
<listbox width="100px">
<listitem label="${each}" forEach="${contacts}"/>

When evaluating the element with the forEach attribute, the each variable is assigned one-by-one with objects from the collection, i.e., contacts in the previous example. Thus, the above ZUML page is the same as follows.

<listitem label="Monday"/>
<listitem label="Tuesday"/>
<listitem label="Wednesday"/>

Alternatively, you can specify a list of items in the forEach attribute by separating them with comma.

<listitem label="${each}" forEach="Monday, Tuesday, Wednesday"/>

In additions to forEach, you can control the iteration with forEachBegin and forEachEnd. Refer to the ZK Attributes section in the ZK User Interface Markup Language chapter for details.