-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Heres a quick example on how to do databinding with ZK:
test.zul:
<zk> <window id="win" title="Testing..." border="normal" apply="test.TestController"> <vlayout> <listbox id="list" model="@{customers}"> <listhead> <listheader label="First" /> <listheader label="Second" /> <listheader label="Check" /> </listhead> <listitem self="@{each=item}" value="@{item}" > <listcell label="@{item.firstName}" /> <listcell label="@{item.lastName}" /> <listcell label="@{item.married}" /> </listitem> </listbox> <separator bar="true" /> <grid id="grid" width="300px"> <rows> <row> <label>First:</label> <textbox id="first" value="@{fixed.firstName}" /> </row> <row> <label>Second:</label> <textbox id="Second" value="@{fixed.lastName}" /> </row> <row> <checkbox id="married" label="Married" disabled="true" checked="@{fixed.married}" /> </row> </rows> </grid> </vlayout> </window> </zk>
TestController.java
package test; import java.util.ArrayList; import java.util.Collection; import java.util.List; import org.zkoss.zk.ui.Component; import org.zkoss.zk.ui.event.Event; import org.zkoss.zk.ui.util.GenericForwardComposer; import org.zkoss.zkplus.databind.AnnotateDataBinder; import org.zkoss.zkplus.databind.Binding; import org.zkoss.zul.Listbox; public class TestController extends GenericForwardComposer { private List customers = new ArrayList(); private Person fixed; private AnnotateDataBinder binder; private Listbox list; public TestController( ) {} @Override public void doAfterCompose( Component win ) throws Exception { super.doAfterCompose(win); // create binder binder = new AnnotateDataBinder(win); // create data for listbox for (int i = 0; i < 10; i++) { boolean married = false; java.util.Random rn = new java.util.Random(); int r = rn.nextInt(10); if (r > 5) married = true; customers.add(new Person(randomstring(4, 6), randomstring(4, 6), married)); } // create a person fixed = new Person(); // bind the data binder.bindBean("customers", customers); binder.bindBean("fixed", fixed); binder.loadAll(); } public void onSelect$list( Event event ) { Person selected = (Person)list.getSelectedItem().getValue(); fixed.setFirstName(selected.getFirstName()); fixed.setLastName(selected.getLastName()); fixed.setMarried(selected.isMarried()); loadBeans("fixed"); } public void loadBeans( String... beans ) { Collection allBindings = binder.getAllBindings(); List<Component> comps = new ArrayList<Component>(); for (Object o : allBindings) { Binding b = (Binding)o; for (String bean : beans) { if (b.getExpression().startsWith(bean + ".") || b.getExpression().equals(bean)) { comps.add(b.getComponent()); break; } } } for (Component c : comps) { binder.loadComponent(c); } } public static String randomstring( int lo, int hi ) { int n = rand(lo, hi); byte b[] = new byte; for (int i = 0; i < n; i++) b<i > = (byte)rand('a', 'z'); return new String(b, 0); } private static int rand( int lo, int hi ) { java.util.Random rn = new java.util.Random(); int n = hi - lo + 1; int i = rn.nextInt(n); if (i < 0) i = -i; return lo + i; } }
Person.java
package test; public class Person { private String firstName = ""; private String lastName = ""; private boolean married = true; public Person( ) { } public Person( String firstName, String lastName, boolean married ) { this.firstName = firstName; this.lastName = lastName; this.married = married; } public boolean isMarried( ) { return married; } public void setMarried( boolean married ) { this.married = married; } public String getFirstName( ) { return firstName; } public void setFirstName( String firstName ) { this.firstName = firstName; } public void setLastName( String lastName ) { this.lastName = lastName; } public String getLastName( ) { return lastName; } // getter and setters public void setFullName( String f ) { // do nothing } public String getFullName( ) { return firstName + " " + lastName; } }
Asked: 2011-02-20 17:45:44 +0800
Seen: 673 times
Last updated: Feb 20 '11