-
FEATURED COMPONENTS
First time here? Check out the FAQ!
What I am trying to do is to bind a list of POJO into a Listbox using DataBinder, and then set the Listbox to allow multiple selection, and then try to loop through the items selected using the Listbox.getSelectedItems() method. However, when I did that, the individual Listitem objects returned from getSelectedItems() are all null.
Anyone can help?
Here is an simplified version of my code:
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit"?> <?page id="test"?> <window id="win" style="margin-top:10px;margin-left:5px;margin-right:5px" xmlns:h="http://www.w3.org/1999/xhtml"> <zscript><![CDATA[ import org.zkoss.zul.event.*; import org.zkoss.zkplus.databind.*; import ork.zkoss.zul.Listbox; import org.zkoss.zul.Listitem; ArrayList list = new ArrayList(); com.myobject.CNote cn; cn = new CNote(); cn.setCNoteCode("CN1"); cn.setCNoteSeq(1); list.add(cn); cn = new CNote(); cn.setCNoteCode("CN2"); cn.setCNoteSeq(2); list.add(cn); cn = new CNote(); cn.setCNoteCode("CN3"); cn.setCNoteSeq(3); list.add(cn); List lvView = list; BindingListModel lvViewM = new BindingListModelList(lvView, true); void doClickAction() { alert("selectedCount="+lbView.getSelectedCount()); for (Listitem li : lbView.getSelectedItems()) { lbView.renderItem(li); alert(li.getValue() + " - " + li.getLabel()); } } ]]></zscript> <vbox width="100%" style="padding:2px"> <hbox><label value="Selected one: "/><textbox id="tx"/></hbox> <listbox id="lbView" model="${lvViewM}" selectedItem="@{selView}" multiple="true" checkmark="true" onSelect="tx.setValue(selView.getCNoteCode());"> <listhead sizable="true"> <listheader width="50%" label="Code" /> <listheader width="50%" label="Seq"/> </listhead> <listitem self="@{each='curr'}" sclass="cells"> <listcell><label value="@{curr.cNoteCode}"/></listcell> <listcell><label value="@{curr.cNoteSeq}"/></listcell> </listitem> </listbox> <button id="bt1" label="Click Action" onClick="doClickAction()"/> </vbox> </window>
When it is run, the textbox can get the Code from newly selected item everytime. And Listbox.getSelectedCount() is also correct. But li.getValue() will always return null, instead of the CNote object I am interested. Please help.
You need to assign the domain object to each listitem:
... <listitem self="@{each='curr'}" sclass="cells" value="@{curr}"> ...
Currently, you're only assigning values from each domain object to the labels in listcells, but you're not assigning the domain object itself to anything. Add the value= and it will give the list item a reference to the domain object for which it was rendered.
Asked: 2009-11-18 09:47:26 +0800
Seen: 3,208 times
Last updated: Nov 18 '09