0

Problem with databinding collections

asked 2010-02-09 08:24:50 +0800

marcio0 gravatar image marcio0
60 1

Hello everybody,
I'm having a problem setting a databiding for an ArrayList in a listbox.

<!-- person.getPhones() returns a List<Phone>, the variable person is in page scope -->
<listbox id="phones" width="200px" rows="4" model="${person.phones}">
	<listhead>
		<listheader label="type" />
		<listheader label="number" />
	</listhead>
	<listitem self="@{each='phone'}">
		<listcell>
			<label value="@{phone.type}"  />
		</listcell>
		<listcell>
			<label value="@{phone.phoneNumber}" />
		</listcell>
	</listitem>
</listbox>

Person.java

public class Person{
   private List<Phone> phones;
   ...
}

Phone.java

public class Phone{
   private String type;
   private String phoneNumber;

As I can access other person attributes (like person.name) in the page, I'm trying to set the model for the listbox as person.phones. Is it right?
Is there something wrong in this code?

delete flag offensive retag edit

11 Replies

Sort by ยป oldest newest

answered 2010-02-09 08:56:02 +0800

clauberferreira gravatar image clauberferreira
27 1

instead use model, try use

<listbox id="phones" width="200px" rows="4" forEach="${person.phones}">

link publish delete flag offensive edit

answered 2010-02-09 09:35:55 +0800

marcio0 gravatar image marcio0
60 1

It works! Cheers!
Thanks for the help!

link publish delete flag offensive edit

answered 2010-02-09 09:49:44 +0800

caclark gravatar image caclark
1753 2 5
http://clarktrips.intltwi...

I don't see how it works. Using forEach, the listbox would be rendered N number of times. Using databinding, the evaluation is done much later. Using the forEach method would require you to populate the phones in the person instance quite early.

I would have expected this to cure your problem:

   <listbox id="phones" width="200px" rows="4" model="@{person.phones}">

I think you should use a debugging tool and see how the phone UI element(s) are really being rendered.

link publish delete flag offensive edit

answered 2010-02-09 10:49:07 +0800

marcio0 gravatar image marcio0
60 1

Claclark, the code in the first post is wrong, it was returning error 500, and I didn't fix it before copying it.
The code you posted is what I was using before, but it didn't work too. I saw the code in this tutorial: http://docs.zkoss.org/wiki/Data_binding
I also tried @<{person.phones>}, but it was a shot in the dark, and still didn't work.

link publish delete flag offensive edit

answered 2010-02-09 19:14:50 +0800

tmillsclare gravatar image tmillsclare
799 2 5 30

Hey marcio0,

Just checking, in your bean do you have the function:

public List<Phone> getPhones() {
     return this.phones;
}

link publish delete flag offensive edit

answered 2010-02-10 06:01:52 +0800

marcio0 gravatar image marcio0
60 1

Tmillsclare, yes, it does.

I'm following this tutorial: http://docs.zkoss.org/wiki/Data_binding#Associate_UI_Components_with_a_Collection

I found more information about the forEach method in this one: http://docs.zkoss.org/wiki/Data_Binding_Collection_Data_with_ZUML_Annotations
But it seems to be older than the first.

link publish delete flag offensive edit

answered 2010-02-22 03:28:41 +0800

tmillsclare gravatar image tmillsclare
799 2 5 30

Hey Marcio,

I think I may know what is going wrong. Are you using zscript? If not are you using the apply="MyComposer" attribute on either the listbox or one of its parents and extending GenericForwardComposer? The latter refer to using ZK in an MVC compliant manner, if not then that is your problem. For more information on using ZK in an MVC manner please take a look at http://docs.zkoss.org/wiki/MVC_in_ZK.

Please let me know if that helps!

link publish delete flag offensive edit

answered 2010-02-22 06:37:55 +0800

marcio0 gravatar image marcio0
60 1

Hey Tmillsclare, I don't use any zscript, and it had the apply attribute on my window. I decided to use it in the MVC way, databinding the data with variables in my composer instead of data in the session.

link publish delete flag offensive edit

answered 2010-02-22 08:06:13 +0800

caclark gravatar image caclark
1753 2 5
http://clarktrips.intltwi...

So, marcio0, do you have it working?

link publish delete flag offensive edit

answered 2010-02-22 11:43:49 +0800

marcio0 gravatar image marcio0
60 1

Yes, caclark, but not the way I wanted. I had to do a few changes to the project.

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-02-09 08:24:50 +0800

Seen: 320 times

Last updated: Feb 22 '10

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