Iterative Evaluation"
Line 21: | Line 21: | ||
</source> | </source> | ||
− | When the ZK Loader iterates through items of the | + | When the ZK Loader iterates through items of the given collection, it will update two implicit objects: [[ZUML Reference/EL Expressions/Implicit Objects/each|each]] and [[ZUML Reference/EL Expressions/Implicit Objects/forEachStatus|forEachStatus]]. The <tt>each</tt> object represents the item being iterated, while forEachStatus is an instance of <javadoc type="interface">org.zkoss.zk.ui.util.ForEachStatus</javadoc>, from which you could retrieve the index and the previous forEach, if any (nested iterations). |
If you have a variable holding a collection of objects, you can specify it directly in the forEach attribute. For example, assume you have a variable called <tt>grades</tt> as follows. | If you have a variable holding a collection of objects, you can specify it directly in the forEach attribute. For example, assume you have a variable called <tt>grades</tt> as follows. |
Revision as of 09:50, 16 September 2011
forEach
By default, ZK instantiates a component for each XML element. If you would like to generate a collection of components, you could specify the forEach attribute. For example,
<listbox>
<listitem label="${each}" forEach="Apple, Orange, Strawberry"/>
</listbox>
is equivalent to
<listbox>
<listitem label="Apple"/>
<listitem label="Orange"/>
<listitem label="Strawberry"/>
</listbox>
When the ZK Loader iterates through items of the given collection, it will update two implicit objects: each and forEachStatus. The each object represents the item being iterated, while forEachStatus is an instance of ForEachStatus, from which you could retrieve the index and the previous forEach, if any (nested iterations).
If you have a variable holding a collection of objects, you can specify it directly in the forEach attribute. For example, assume you have a variable called grades as follows.
grades = new String[] {"Best", "Better", "Good"};
Then, you can iterate them by use of the forEach attribute as follows. Notice that you have to use EL expression to specify the collection.
<listbox>
<listitem label="${each}" forEach="${grades}"/>
</listitem>
The iteration depends on the type of the value of the forEach attribute:
- If java.util.Collection, it iterates each element of the collection.
- If java.util.Map, it iterates each Map.Entry of the map.
- If java.util.Iterator, it iterates each element from the iterator.
- If java.util.Enumeration, it iterates each element from the enumeration.
- If Object[], int[], short[], byte[], char[], float[] or double[] is specified, it iterates each element from the array.
- If null, nothing is generated (it is ignored).
- If neither of the above types is specified, the associated element will be evaluated once as if a collection with a single item is specified.
The each Object
During the evaluation, an object called each is created and assigned with the item from the specified collection. In the above example, each is assigned with "Best" in the first iteration, then "Better" and finally "Good".
Notice that the each object is accessible both in EL expression and in zscript. ZK will preserve the value of the each object if it is defined before, and restore it after the evaluation of the associated element.
The forEachStatus Object
The forEachStatus object is an instance of ForEachStatus. It holds the information about the current iteration. It is mainly used to get the item of the enclosing element that is also assigned with the forEach attribute.
In the following example, we use nested iterative elements to generate two listboxes.
<hlayout>
<zscript>
classes = new String[] {"College", "Graduate"};
grades = new Object[] {
new String[] {"Best", "Better"}, new String[] {"A++", "A+", "A"}
};
</zscript>
<listbox width="200px" forEach="${classes}">
<listhead>
<listheader label="${each}"/>
</listhead>
<listitem label="${forEachStatus.previous.each}: ${each}"
forEach="${grades[forEachStatus.previous.index]}"/>
</listbox>
</hlayout>
Notice that the each and forEachStatus objects are accessible both in EL expression and in zscript.
Apply forEach to Muliple Elements
If you have to iterate a collection of items for multiple XML elements, you could group them with the zk element as shown below.
<zk forEach="${cond}">
${each.name}
<textbox value="${each.value}"/>
<button label="Submit"/>
</zk>
The zk
element is a special element used to group a set of XML element nested. ZK Loader won't create a component for it. Rather, ZK Loader interprets the forEach, if and unless attribute it might have.
Access each and forEachStatus in Java
You could access the each and forEachStatus object directly in zscript such as:
<window>
<button label="${each}" forEach="apple, orange">
<zscript>
self.parent.appendChild(new Label("" + each));
</zscript>
</button>
</window>
In a composer, you could retrieve them from the attributes, because these objects are actually stored in the parent component's attributes (Component.getAttribute(String)). For example,
public class Foo implements Composer {
public void doAfterCompose(Component comp) throws Exception {
Object each = comp.getParent().getAttribute("each"); //retrieve the each object
ForEachStatus forEachStatus = (ForEachStatus)comp.getParent().getAttribute("forEachStatus");
//...
}
}
If the component is a root, you could retrieve them from page's attributes (Page.getAttribute(String)).
Access each and forEachStatus in Event Listeners
However, you cannot access the values of each and forEachStatus in an event listener because their values are reset after the XML element which forEach is associated has been evaluated.
For example, the following code won't work:
<button label="${each}" forEach="${countries}"
onClick="alert(each)"/> <!-- incorrect!! -->
When the onClick event is received, the each object no longer exists.
There is a simple solution: store the value in the component's attribute, so you can retrieve it when the event listener is called. For example,
<button label="${each}" forEach="${countries}"
onClick='alert(self.getAttribute("country"))'>
<custom-attributes country="${each}"/>
</button>
Iterate a Subset of a Collection
If you'd like to iterate a subset of a collection, you could specify the forEachBegin and/or forEachEnd attributes.
<grid>
<rows>
<row forEach="${foos}" forEachBegin="${param.begin}" forEachEnd="${param.end}">
${each.name} ${each.title}
</row>
</rows>
</grid>
Version History
Version | Date | Content |
---|---|---|