0

Listbox.getSelectedItems(), then Listitem.getValue() returns null?

asked 2009-11-18 09:47:26 +0800

unixp1971 gravatar image unixp1971
6 1 1

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.

delete flag offensive retag edit

2 Replies

Sort by ยป oldest newest

answered 2009-11-18 10:01:41 +0800

caclark gravatar image caclark
1753 2 5
http://clarktrips.intltwi...

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.

link publish delete flag offensive edit

answered 2009-11-18 10:09:53 +0800

unixp1971 gravatar image unixp1971
6 1 1

That's it. Many thanks.

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
2 followers

RSS

Stats

Asked: 2009-11-18 09:47:26 +0800

Seen: 3,208 times

Last updated: Nov 18 '09

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