-
FEATURED COMPONENTS
First time here? Check out the FAQ!
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?
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();
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).
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
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");
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
Can anyone answer my question related to Databinding here
Asked: 2009-06-14 05:55:00 +0800
Seen: 4,945 times
Last updated: Dec 11 '12