foreach"

From Documentation
(Created page with '{{ZKDevelopersGuidePageHeader}} It's a common usage of <tt>zscript</tt> to define array of object for <tt>forEach</tt> <source lang="xml" > <window> <zscript><![CDATA[ contac…')
 
m (correct highlight (via JWB))
 
Line 1: Line 1:
 
{{ZKDevelopersGuidePageHeader}}
 
{{ZKDevelopersGuidePageHeader}}
  
It's a common usage of <tt>zscript</tt> to define array of object for <tt>forEach</tt>
+
It's a common usage of <code>zscript</code> to define array of object for <code>forEach</code>
  
 
<source lang="xml" >
 
<source lang="xml" >
Line 15: Line 15:
 
</source>
 
</source>
  
You can move the definition of array of object to java file as the following example. But remember to add <tt>id</tt> inside <tt>${}</tt>, then EL knows which component has the variable.
+
You can move the definition of array of object to java file as the following example. But remember to add <code>id</code> inside <code>${}</code>, then EL knows which component has the variable.
  
 
<source lang="xml" >
 
<source lang="xml" >
Line 36: Line 36:
 
</source>
 
</source>
  
Or you can use <tt>appendChild</tt>. Manually generate all children in java, then you don't have to mix with <tt>forEach</tt> in ZUML. In the following example, the <tt>listitem</tt>s will be appended to <tt>listbox</tt> after the <tt>button</tt> is clicked.
+
Or you can use <code>appendChild</code>. Manually generate all children in java, then you don't have to mix with <code>forEach</code> in ZUML. In the following example, the <code>listitem</code>s will be appended to <code>listbox</code> after the <code>button</code> is clicked.
 
<source lang="xml" >
 
<source lang="xml" >
 
<window id="win_1" use="MyWindow" title="list">
 
<window id="win_1" use="MyWindow" title="list">

Latest revision as of 10:41, 19 January 2022

Stop.png This documentation is for an older version of ZK. For the latest one, please click here.


It's a common usage of zscript to define array of object for forEach

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

You can move the definition of array of object to java file as the following example. But remember to add id inside ${}, then EL knows which component has the variable.

<window id="win_1" use="MyWindow">
	<listbox>
		<listitem label="${each}" forEach="${win_1.contacts}"/>
	</listbox>
</window>
import org.zkoss.zul.Window;
 
public class MyWindow extends Window {
	String[] contacts = new String[] {"Monday", "Tuesday", "Wednesday"};
	public String[] getContacts(){
		return contacts;
	}
}

Or you can use appendChild. Manually generate all children in java, then you don't have to mix with forEach in ZUML. In the following example, the listitems will be appended to listbox after the button is clicked.

<window id="win_1" use="MyWindow" title="list">
	<listbox id="lb_1">
	</listbox>
	<button label="Hello" onClick="win_1.onTest()"/>
</window>
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Window;

public class MyWindow extends Window {
	String[] contacts = new String[] { "Monday", "Tuesday", "Wednesday" };

	public void onTest() {
		Listbox lb = (Listbox) getFellow("lb_1");
		for (int i = 0; i < contacts.length; i++) {
			Listitem li = new Listitem();
			li.setLabel(contacts[i]);
			lb.appendChild(li);
		}
	}
}



Last Update : 2022/01/19

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.