each"
m |
|||
(5 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
{{ZUMLReferencePageHeader}} | {{ZUMLReferencePageHeader}} | ||
− | = each | + | = each = |
The current item of the collection being iterated, when ZK evaluates an iterative element. An iterative element is an element with the forEach attribute. | The current item of the collection being iterated, when ZK evaluates an iterative element. An iterative element is an element with the forEach attribute. | ||
Line 11: | Line 11: | ||
== Nested forEach == | == Nested forEach == | ||
− | To retrieve the index of the iteration, or the previous < | + | To retrieve the index of the iteration, or the previous <code>each</code> object in nested forEach, you have to use another implicit object called [[ZUML Reference/EL Expressions/Implicit Objects/forEachStatus|forEachStatus]]. |
+ | |||
+ | <source lang="xml"> | ||
+ | <listbox forEach="${matrix}"> | ||
+ | <listitem label="${forEachStatus.previous.each.label}: ${each}" forEach=${each.items}/> <!-- nested--> | ||
+ | </listbox> | ||
+ | </source> | ||
== In Java == | == In Java == | ||
− | You could access the < | + | You could access the <code>each</code> object directly in zscript such as: |
<source lang="xml"> | <source lang="xml"> | ||
<window> | <window> | ||
Line 26: | Line 32: | ||
</source> | </source> | ||
− | + | The <code>each</code> object is actually stored in the parent component's attribute, so you could retrieve it in pure Java as follows. | |
− | |||
− | The < | ||
<source lang="java"> | <source lang="java"> | ||
Line 34: | Line 38: | ||
public void doAfterCompose(Component comp) throws Exception { | public void doAfterCompose(Component comp) throws Exception { | ||
Object each = comp.getParent().getAttribute("each"); //retrieve the each object | Object each = comp.getParent().getAttribute("each"); //retrieve the each object | ||
− | ForEachStatus forEachStatus = (ForEachStatus)comp.getParent().getAttribute("forEachStatus"); | + | ForEachStatus forEachStatus = (ForEachStatus)comp.getParent().getAttribute("forEachStatus"); |
//... | //... | ||
} | } | ||
Line 41: | Line 45: | ||
If the component is a root, you could retrieve them from page's attributes (<javadoc method="getAttribute(java.lang.String)" type="interface">org.zkoss.zk.ui.Page</javadoc>). | If the component is a root, you could retrieve them from page's attributes (<javadoc method="getAttribute(java.lang.String)" type="interface">org.zkoss.zk.ui.Page</javadoc>). | ||
+ | |||
+ | However, the value of <code>each</code> is reset after the XML element that <code>forEach</code> is associated has been evaluated. Thus, you cannot access it in an event listener, unless you store the value first. For more information, please refer to [[ZK Developer's Reference/UI Composing/ZUML/Iterative Evaluation|ZK Developer's Reference: Iterative Evaluation]]. | ||
=Version History= | =Version History= |
Latest revision as of 03:17, 15 April 2024
each
The current item of the collection being iterated, when ZK evaluates an iterative element. An iterative element is an element with the forEach attribute.
<listbox width="100px">
<listitem label="${each}" forEach="${contacts}" />
</listbox>
Nested forEach
To retrieve the index of the iteration, or the previous each
object in nested forEach, you have to use another implicit object called forEachStatus.
<listbox forEach="${matrix}">
<listitem label="${forEachStatus.previous.each.label}: ${each}" forEach=${each.items}/> <!-- nested-->
</listbox>
In Java
You could access the each
object directly in zscript such as:
<window>
<button label="${each}" forEach="apple, orange">
<zscript>
self.parent.appendChild(new Label("" + each));
</zscript>
</button>
</window>
The each
object is actually stored in the parent component's attribute, so you could retrieve it in pure Java as follows.
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)).
However, the value of each
is reset after the XML element that forEach
is associated has been evaluated. Thus, you cannot access it in an event listener, unless you store the value first. For more information, please refer to ZK Developer's Reference: Iterative Evaluation.
Version History
Version | Date | Content |
---|---|---|