0

Databinding Listbox does not display selectedItem

asked 2011-04-13 16:33:14 +0800

mhongslo gravatar image mhongslo
51 1

updated 2011-04-13 17:03:38 +0800

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>


The controller code:
    @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;
    }



My controller has a method called getProviders() which calls the service layer to obtain the items from the database.


When is 'org.zkoss.zkplus.databind.SelectedItemConverter' supposed to be used? The javadocs on this doesn't provide much in details.
Removing the converter doesn't seem to make a difference in the result. Do I need to use this converter for this example?

Thanks,
Mike

delete flag offensive retag edit

5 Replies

Sort by ยป oldest newest

answered 2011-04-13 17:22:59 +0800

cbclark64 gravatar image cbclark64
50 1

I'm going to go out on a limb and suggest you add super.doAfterCompose(...) at the beginning of doAfterCompose(...).

link publish delete flag offensive edit

answered 2011-04-13 17:28:00 +0800

robertpic71 gravatar image robertpic71
1275 1

updated 2011-04-13 17:31:16 +0800

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

link publish delete flag offensive edit

answered 2011-04-13 17:34:58 +0800

mhongslo gravatar image mhongslo
51 1

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

link publish delete flag offensive edit

answered 2011-04-13 18:50:32 +0800

cbclark64 gravatar image cbclark64
50 1

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

link publish delete flag offensive edit

answered 2011-04-13 18:55:35 +0800

mhongslo gravatar image mhongslo
51 1

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

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: 2011-04-13 16:33:14 +0800

Seen: 703 times

Last updated: Apr 13 '11

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