-
FEATURED COMPONENTS
First time here? Check out the FAQ!
I have multi persons pojo with multi addresses. Is there a way to list all persons and their corresponding addresses, or return the first element of the array of the addresses? Is my controller, I would have:
public class EditAttorneyController extends GenericForwardComposer { List<Person> persons; @Override public void doAfterCompose(Component comp) throws Exception { super.doAfterCompose(comp); comp.setAttribute("controller", this); //populate persons persons = DAO.getAllPersons(); } }
My zul:
<listbox id="personsLb" multiple="false" model="@{controller.persons}"> <listitem self="@{each='person'}"> <listcell><label="@{person.name}"/></listcell> <listcell><label="@{person.addresses[0].street1}"/></listcell> </listitem> </listbox>
ZK does not return the first element [0] of the array but returns the whole array. Is there a way to pick off the first element or have another self="@{each='addresses'}" nested inside the listbox?
Hi xnguyen
The collection only can set to the model when using databinding
please refer to here
You can use TypeConverter
Composer.java
package ctrl; import java.util.*; import org.zkoss.zk.ui.Component; import org.zkoss.zk.ui.util.GenericForwardComposer; public class Composer extends GenericForwardComposer { List<Person> persons; @Override public void doAfterCompose(Component comp) throws Exception { super.doAfterCompose(comp); initData(); } private void initData() { persons = new ArrayList<Person>(); persons.add(new Person("Jimmy", new String[]{"Wisconsin","San Francisco"})); persons.add(new Person("Katrina", new String[]{"New York","Las Vegas"})); } public List<Person> getPersons(){ return persons; } public class Person{ private String name; private String[] addresses; public Person(String name, String[] addresses) { super(); this.name = name; this.addresses = addresses; } public String getName() { return name; } public String[] getAddresses() { return addresses; } } }
MyConverter.java
package view; import org.zkoss.zk.ui.Component; import org.zkoss.zkplus.databind.TypeConverter; public class MyConverter implements TypeConverter { @Override public Object coerceToBean(Object val, Component comp) { return null; } @Override public Object coerceToUi(Object val, Component comp) { String[] addresses = (String[]) val; return addresses[0]; } }
index.zul
<?page title="new page title" contentType="text/html;charset=UTF-8"?> <?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit"?> <zk> <window border="none" id="win" apply="ctrl.Composer"> <listbox id="personsLb" multiple="false" model="@{win$Composer.persons}"> <listitem self="@{each='person'}" > <listcell label="@{person.name}"/> <listcell label="@{person.addresses, converter='view.MyConverter'}" /> </listitem> </listbox> </window> </zk>
My Issue is like the above, but My data source type is " List<Object[]> ".
The Object[] contents are like :
obj[0] = new Project(); obj[1] = new String("project manager name"); ......
and my zul file code is simply like:
...... <listbox id="projListbox" model="projList"> <listhead sizable="true"> <listheader label="Project code" /> <listheader label="Project name" /> <listheader label="Project Manager name" /> ...... </listhead> <listitem self="@{each='proj' }"> <listcell label="@{proj[0].code }"/> <listcell label="@{proj[0].name }"/> <listcell label="@{proj[1] }"/> ...... </listitem> </listbox> ......
But seems I can't retrieve the properties of the beans by the data-binding annotation shown above.
So Combining the beans into 1 custom bean used only in UI is the only way I can use ?
Do I have better solutions in this situation?
Thanks, and Sorry about my poor English. ><"
@wiselytwu,
Write TypeConverter that will convert proj to proj[0].code for first column...
public class PrjCodeConverter implements TypeConverter { @Override public Object coerceToBean(Object val, Component comp) { return null; } @Override public Object coerceToUi(Object val, Component comp) { Object[] proj = (Object[]) val; return ((Proejct)proj[0]).getCode(); } }
... <listitem self="@{each='proj'}"> <listcell label="@{proj, converter="view.PrjCodeConverter"}"/> ... </listitem>
Asked: 2010-05-17 19:50:15 +0800
Seen: 923 times
Last updated: Jun 26 '11