each"
m |
m |
||
Line 9: | Line 9: | ||
</listbox> | </listbox> | ||
</source> | </source> | ||
+ | |||
+ | == Nested forEach == | ||
+ | To retrieve the index of the iteration, or the previous <tt>each</tt> object in nested forEach, you have to use another implicit object called [[ZUML Reference/EL Expressions/Implicit Objects/forEachStatus|forEachStatus]]. | ||
+ | |||
+ | == In Java == | ||
+ | |||
+ | You could access the <tt>each</tt> object directly in zscript such as: | ||
+ | <source lang="xml"> | ||
+ | <window> | ||
+ | <button label="${each}" forEach="apple, orange"> | ||
+ | <zscript> | ||
+ | self.parent.appendChild(new Label("" + each)); | ||
+ | </zscript> | ||
+ | </button> | ||
+ | </window> | ||
+ | </source> | ||
+ | |||
+ | However, the value of <tt>each</tt> is reset after the XML element that <tt>forEach</tt> is associated has been evaluated. Thus, you cannot access it in an event listener, unless you store the value first. | ||
+ | |||
+ | The <tt>each</tt> object is actually stored in the parent component's attribute, so you could retrieve it in pure Java as follows. | ||
+ | |||
+ | <source lang="java"> | ||
+ | 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"); //status | ||
+ | //... | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | 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>). | ||
=Version History= | =Version History= |
Revision as of 03:05, 6 December 2010
each - java.lang.Object
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.
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>
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.
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"); //status
//...
}
}
If the component is a root, you could retrieve them from page's attributes (Page.getAttribute(String)).
Version History
Version | Date | Content |
---|---|---|