0

Get Multiselcted items from listbox and annotation

asked 2009-04-16 12:11:37 +0800

fusion35 gravatar image fusion35
237 2 5

Hi :

zul snippet :

<listbox id="lbCategory" width="350px" multiple="true" checkmark="true" rows="4" selectedItems="@{applicant.applicantGroup}"/>

The model :


private transient Set<MasterApplicantGroup> applicantGroup;

public Set<MasterApplicantGroup> getApplicantGroup() {
   return applicantGroup;
}

public void setApplicantGroup(Set<MasterApplicantGroup> applicantGroup) {
    this.applicantGroup = applicantGroup;
}


In my controller when I do binder.loadAll() it gives me a Nullpointer exception. If applicantGroup is initalized then the null pointer goes away but binder.saveAll() doesn't have the selectedItems in it.

What wrong am I doing?

regards

Devinder

delete flag offensive retag edit

23 Replies

Sort by ยป oldest newest

answered 2009-04-17 01:29:36 +0800

iantsai gravatar image iantsai
2755 1

Because your listbox didn't contain anything.

And I'm not sure that Annotated Databinding support multiple selection in listbox.

If your requirement need to do a lot of detail control of Listbox, then using Model + renderer would be safety.

Sometimes it's better to choose the folksy approach.

link publish delete flag offensive edit

answered 2009-04-17 03:43:41 +0800

fusion35 gravatar image fusion35
237 2 5

Thanks for the reply.

>>Because your listbox didn't contain anything.

I have it, both model and renderer are set in the controller.

    public void onCreate$applicantWindow() {
        try {
            lbCategory.setModel(new ListModelList(ApiManager.getMasterGroupDao().readAllMasterGroup()));
            lbCategory.setItemRenderer(new SimpleListboxRenderer());

            applicant = (MasterApplicant) params.get("applicant");

            binder = new AnnotateDataBinder(this);
            binder.bindBean("applicant", applicant);
            binder.loadAll();

        } catch (DAOException ex) {
            log.error("onCreate$applicantWindow : "+ex.getMessage());
        }
    }

To make it work I have taken away the selectedItems="@{applicant.applicantGroup}" from the list box and set the bean value manually (using getSelecteItems()). My question remains how do I get the values using annotation?

regards

Devinder

link publish delete flag offensive edit

answered 2009-04-20 01:09:26 +0800

iantsai gravatar image iantsai
2755 1

Ok, now I see your problem.

Instead of ListModelList, maybe you should try this one:
org.zkoss.zkplus.databind.BindingListModelList

link publish delete flag offensive edit

answered 2009-04-20 14:09:27 +0800

fusion35 gravatar image fusion35
237 2 5

Thanks for the reply.

That doesn't work either.

regards

Devinder

link publish delete flag offensive edit

answered 2009-04-21 01:38:48 +0800

iantsai gravatar image iantsai
2755 1

Are you sure "applicant" is a instance from ApiManager.getMasterGroupDao().readAllMasterGroup()?

If the instances are different then there's nothing Binder can do.

link publish delete flag offensive edit

answered 2009-04-21 10:36:20 +0800

fusion35 gravatar image fusion35
237 2 5

Thanks for the reply.

No. "applicant" is an instance of "MasterApplicant"

 applicant = (MasterApplicant) params.get("applicant");

In MasterApplicant (my model) I have a class variable :

 private transient Set<MasterApplicantGroup> applicantGroup;

to which I am referring in zul file as :
<listbox id="lbCategory" width="350px" multiple="true" checkmark="true" rows="4" selectedItems="@{applicant.applicantGroup}"/>

Is there something that I am missing?

regards

Devinder

link publish delete flag offensive edit

answered 2009-04-22 01:58:15 +0800

iantsai gravatar image iantsai
2755 1

updated 2009-04-22 01:59:21 +0800

Finally I have checked the Javadoc, and now I sorry say: "you can't make it in this approach."
because there's no "setSelectedItems(Set selected)" method in listbox!


So now the way to do your job is control it all by your self. In this complex situation I think we should forget Annotated Databinding.
This technology doesn't feet this situation.


    public void onCreate$applicantWindow() {
        try {
            ListModelList groupModel = new ListModelList(ApiManager.getMasterGroupDao().readAllMasterGroup());
            lbCategory.setModel(groupModel );
            lbCategory.setItemRenderer(new SimpleListboxRenderer());
            applicant = (MasterApplicant) params.get("applicant");
            Listitem item;
            for(MasterApplicantGroup group : applicant.getApplicantGroup()){
                item = lbCategory.getItemAtIndex(groupModel.indexOf(group))
                lbCategory.addItemToSelection(item);
            }
            
            
        } catch (DAOException ex) {
            log.error("onCreate$applicantWindow : "+ex.getMessage());
        }
    }


link publish delete flag offensive edit

answered 2009-04-22 03:22:49 +0800

robertpic71 gravatar image robertpic71
1275 1

Multiple databinding with annotations is only available with an own converter.
The user dasultz has written one.

<listbox id="lbCategory" width="350px" multiple="true" checkmark="true" rows="4" selectedItems="@{applicant.applicantGroup, converter=daslutz.magiccode}"/>

Check this thread.

Post if you have any troubles - i do not need multiple selection until now... so i have not testet the code. But user dasultz seems to be a poweruser, so i think the code shoudl work.

Or use the non-databinding-way as descriped by Iantsai.

/Robert

link publish delete flag offensive edit

answered 2009-04-22 04:38:12 +0800

fusion35 gravatar image fusion35
237 2 5

updated 2009-04-23 14:02:39 +0800

Thanks guys for the reply.

Iantsai, there is a setSelectedItems method in listbox which accepts set (since 3.6.0, a new addition I guess)

public void setSelectedItems(java.util.Set listItems)

which is why my model variable is a set.

private transient Set<MasterApplicantGroup> applicantGroup;

I was thinking a mere selectedItems="@{applicant.applicantGroup} should work. I will also to look into the Dasulz's approach of converter, as suggested by Robert.

I am already using manual approach as you too suggested, but would definitely like to see the annotation working.

regards

Devinder

link publish delete flag offensive edit

answered 2009-04-29 17:52:51 +0800

shumy gravatar image shumy
244 1

Solution provided by robertpic71 won't work because the listbox doesn't update the selectedItems wen items are selected.
And the converter fails sometimes wen listbox has listhead's or other different child components.
Also,

public void setSelectedItems(java.util.Set listItems)
is not in the org.zkoss.zul.api.Listbox interface.

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: 2009-04-16 12:11:37 +0800

Seen: 3,740 times

Last updated: Jul 01 '10

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