0

2 dimensional array in databinding with expressions

asked 2010-05-17 19:50:15 +0800

xnguyen gravatar image xnguyen
30 3

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?

delete flag offensive retag edit

4 Replies

Sort by ยป oldest newest

answered 2010-05-18 06:12:26 +0800

jimmyshiau gravatar image jimmyshiau
4921 5
http://www.zkoss.org/ ZK Team

updated 2010-05-18 06:18:05 +0800

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>

link publish delete flag offensive edit

answered 2010-06-05 22:31:40 +0800

xnguyen gravatar image xnguyen
30 3

works perfectly! thanks as1225 for the help.

link publish delete flag offensive edit

answered 2011-06-20 06:15:02 +0800

wiselytwu gravatar image wiselytwu
3

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. ><"

link publish delete flag offensive edit

answered 2011-06-26 22:22:22 +0800

henrichen gravatar image henrichen
3869 2
ZK Team

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

}

Maybe not perfect, but doable...
...
<listitem self="@{each='proj'}">
    <listcell label="@{proj, converter="view.PrjCodeConverter"}"/>
    ...
</listitem>

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2010-05-17 19:50:15 +0800

Seen: 923 times

Last updated: Jun 26 '11

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More