0

Combobox data binding programmatically

asked 2009-06-14 05:55:00 +0800

romen gravatar image romen
33 1 3

Hi

I have the following zml code:

<combobox model="${weeklyModel}" selectedItem="@{selected.timeRange.end}"
  value="@{selected.timeRange.end, converter='PartialTypeConverter'}"
  readonly="true">
  <comboitem self="@{each=endd}" 
    label="@{endd, converter='PartialTypeConverter'}" 
    value="@{endd}"/>
</combobox>

I want to achieve the above programmatically using zscript. For example:

Combobox comp=new Combobox();
comp.setModel(weeklyModel);
comp.setId("startComp");
comp.setReadonly(true);
		
DataBinder db=new DataBinder();
db.addBinding(comp, "selectedItem", "selected.timeRange.start");
db.addBinding(comp, "value", "selected.timeRange.start",
	null, null, "both", PartialTypeConverter.class.getName());
...// what do I do with the <comboitem> lines?
db.bindBean("selected", selected);
db.loadAll();

However, I don't know how to turn the <comboitem> into zscript since it has 'each' in it. The result I get from the above zscript code is that the combobox is created, but the items inside has the wrong label (i.e. no type converter is applied). So how should I do this in script or java?

delete flag offensive retag edit

9 Replies

Sort by ยป oldest newest

answered 2009-06-18 02:10:01 +0800

henrichen gravatar image henrichen
3869 2
ZK Team

try

Comboitem item = new Comboitem();
item.setParent(comp);
db.addBinding(item, "each", "endd", null, null, null, null);

link publish delete flag offensive edit

answered 2009-06-18 12:48:34 +0800

romen gravatar image romen
33 1 3

Thanks. That has solved the comboitem label problem. My code now looks like this:

Combobox comp=new Combobox();
startHbox.appendChild(comp);
comp.setModel(weeklyModel);
comp.setId("startComp");
comp.setReadonly(true);
		
DataBinder db=new DataBinder();
db.addBinding(comp, "selectedItem", "selected.timeRange.start");
//db.addBinding(comp, "value", "selected.timeRange.start",
//	null, null, "both", WeeklyTypeConverter.class.getName());
		
Comboitem item=new Comboitem();
item.setParent(comp);
db.addBinding(item, "each", "startt", null, null, "both", null);
db.addBinding(item, "label", "startt", null, null, null, WeeklyTypeConverter.class.getName());
	
db.loadAll();

However, the binding does not quite work.
The saving works - i.e. if I change the combobox's selection, the corresponding bean's value gets changed.

The loading does not work - the combobox will initially load the correct selected item based on the bean's value. However, if I change the bean's value, the combobox does not get updated/refreshed automatically. The combobox will get updated only if I call db.loadAll() explicitly.

I tried to call db.bindBean() but made no difference.

What else am I missing?

cheers
romen

link publish delete flag offensive edit

answered 2009-06-18 13:35:08 +0800

edudant gravatar image edudant
219 1 1 1
zk.datalite.cz

Databinder can't know that you have changed some value in java.

If you change value from Java, you have to tell Databinder, that something has changed:
- reload everything - db.loadAll()
- reload one component - db.loadComponent(comp);
- reload one attribute - db.getBinding(comp, attribute).loadAttribute(comp);

There is an exception - when binding saves some value, it also does refresh on all connected beans (beans on same java objects).

link publish delete flag offensive edit

answered 2009-06-18 23:43:09 +0800

romen gravatar image romen
33 1 3

Hi

Thanks edudant.

I am not changing the bean using java. Rather, my setup is like this:
I have a listbox which is data-bound. Everytime I select a listitem the "selected" bean is changed to match the selection. And I want to populate a form that shows all the fields/properties of the bean and the combobox is one of the fields of the form. (like a master-detail window).

The thing is, if I use zul to construct the combobox (as in my first post), everything works fine. But when I do it programmatically using zscript, I experience the problem of combobox not updating after I select the listbox.

As a workaround I have added an onSelect handler for the listbox to forciblly call loadAll() on the combobox' data binder. But surely there should be a proper way.

cheers
romen

link publish delete flag offensive edit

answered 2009-06-19 06:44:10 +0800

edudant gravatar image edudant
219 1 1 1
zk.datalite.cz

I see. The problem is that you create new databinder.

When you add

  <?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" ?>

The associated code in this class is:

		} else if (_compPath == null || "page".equals(_compPath)) { //page
			_binder = new AnnotateDataBinder(page, b);
			if (page.getVariable("binder") != null) { //already a binder on the page
				throw new UiException("Page is already covered by another Data Binder. Cannot be covered by this Data Binder again. Page:"+page.getId());
			} else {
				page.setVariable("binder", _binder);
			}

as you can see, it creates new databinder and puts it into page variable.

You can provide arg0 parameter as well with id of existing component and the databinder will be stored in that component attribute.

To make automatic refresh work, you need to use same databinder on both components:

   // DataBinder db=new DataBinder(); 
   DataBinder db=page.getVariable("binder"); 

link publish delete flag offensive edit

answered 2009-06-22 05:00:43 +0800

romen gravatar image romen
33 1 3

Thanks. I do have an annotate data binder for my listbox and it has arg0 assigned. How do I programmtically get that data binder?

cheers
romen

link publish delete flag offensive edit

answered 2009-06-22 05:14:20 +0800

romen gravatar image romen
33 1 3

I think I have found my answer in the javadoc of AnnotateDataBinderInit.

But using that binder, when I try to bind the comboitem's "each" attribute programmatically, it returned error:
SEVERE: >>org.zkoss.zk.ui.UiException: Cannot find the specified databind bean expression:start

link publish delete flag offensive edit

answered 2009-06-22 05:21:41 +0800

romen gravatar image romen
33 1 3

sorry, the exception occurs not when I tried to bind "each", but when I tried to bind "label" of the comboitem (code is in 3rd message in this thread).

link publish delete flag offensive edit

answered 2012-12-11 07:45:36 +0800

sjoshi gravatar image sjoshi flag of India
3493 1 8
http://zkframeworkhint.bl...

Can anyone answer my question related to Databinding here

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: 2009-06-14 05:55:00 +0800

Seen: 4,945 times

Last updated: Dec 11 '12

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