-
FEATURED COMPONENTS
First time here? Check out the FAQ!
HI all,
I am using databinding on a Listbox using the "select" mold. My problem is when the window loads,
the listbox doesn't show the correct selected item. Prior to this form, I select the item and then take
the selected item and store in the request object. When loading the next window, this will read the
selected item from the request and set the bean in the controller. In doAfterCompose() I dump out
the value it retrieve and is correct. Here is my code. Why is my listbox not displaying the right
selectedItem? The listbox does show the correct items in the list -- just not showing the selectedItem.
<window id="inputform" title="" border="none" width="min" apply="${MyController}" zclass="testform"> <listbox id="providerlist" mold="select" model="@{inputform$MyControllerr.providers}" selectedItem="@{inputform$MyController.selectedProvider, converter='org.zkoss.zkplus.databind.SelectedItemConverter'}" style="width:190px" zclass="dropdown"> <listitem self="@{each='provider'}" value="@{provider}"> <listcell label="@{provider.fullName}" /> </listitem> </listbox> </window>
@Override public void doAfterCompose(Component window) { setSelectedProvider((Provider)execution.getAttribute("provider")); log.debug("Provider: " + getSelectedProvider().getFirstName()); // log file shows correct firstname } public Provider getSelectedProvider() { return selectedProvider; }
First, you don't have to define the converter, there is a working default convert for the most cases.
1. I did not see any databinder-init. <?init... or binder inisde the controller - maybe it's a timingproblem (setSelected after binder.loadAll).
2. I selectedItems not working, this could mean: wrong or missing equals hashcode methods. If you you i.e. JPA/Hibernate it doesn't work without override the equals.
Post again if you need more detailed infos.
Edit: and of course: call super as cbclark64 wrote... this error should be in the hall of fame
/Robert
My zul files contains the data binding at the top of the file
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" root="./inputform"?>
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
Also,
My doAfterCompose() does have super.doAfterCompose() at the beginning... Sorry for not in cluding this in my posting. Just trying to keep this post short.
Mike
Ok, here's another swipe..... looking closely at your model and selectedItem definitions, I see a difference in spelling of the controller reference:
... model="@{inputform$MyControllerr.providers}" selectedItem="@{inputform$MyController.selectedProvider} ...
model has 2 "r"s in MyControllerr, selectedItem has 1 "r" in MyController
Robert,
After taking your suggestion of checking selecteItems and looking at equals and hashcode for my domain object. Yes - I am using Hibernate. My code had checks for values in foreign keys .... removing those made everything work.
The equals() and hashCode() methods are a big pain when using Hibernate.
Is the general rule to implement equals and hashCode for any selectedItem attribute right? Where is this documented in ZK because I can't find a good documentation of data binding with listboxes. MOst people would know that you need hashCode and equals to make selectedItem work.
Mike
Asked: 2011-04-13 16:33:14 +0800
Seen: 703 times
Last updated: Apr 13 '11