From Documentation

Jump to: navigation, search



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);
		}
	}
}




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