Listbox selectedItem from listItem value attribute

asked 2014-07-23 06:25:59 +0800

yagamipaul gravatar image yagamipaul
15 2

Is it possible to get the selectedItem of a listbox with the type of a specific property of the model's object type?.

An example is like this:

<listbox mold="select" model="@load(vm.countries)" selectedItem="@bind(vm.selectedCountry)">
   <template name="model">
       <listitem label="@load(each.label)" value="@load(each.value)" />

I want to bind the property "selectedCountry" (String) with the "value" property of a listitem element. "selectedCountry" is a String, while the model vm.countries is a list of Country objects with 2 properties, label and value.

The problem is that by doing this in the listitem: value="@load(each.value)", doesn't work. The selectedItem property will be set with the object Country toString and not with the property "value".

Is that possible?, doesn't work with listbox or combobox. I'm using ZK 7.0.2 CE.

delete flag offensive retag edit


At the moment you save "each" => country in selectedCountry. With that you have all what you need, or not?

chillworld ( 2014-07-23 07:31:12 +0800 )edit

Yes, I will have the complete Country object in the selectedCountry reference, but it's not possible to bind only the country.value in selectedCountry?

yagamipaul ( 2014-07-23 19:10:14 +0800 )edit

Hi it is possible but you need to write your own converter from ListItem to String, by default when using a ListModel the listbox applies a converter to the listmodel's type.

cor3000 ( 2014-07-24 02:54:55 +0800 )edit

By default the converter "org.zkoss.bind.converter.sys.ListboxSelectedItemConverter" is configured in zkbind.jar:metainfo/zk/lang-addon.xml, you can use a custom converter in your bind expression @bind(vm.selectedCountryName) @converter('foo.MyCustomConverter')

cor3000 ( 2014-07-24 03:06:10 +0800 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2014-07-23 22:25:03 +0800

chillworld gravatar image chillworld flag of Belgium
5357 4 9

Well, it's possible to get it done, but at what cost.

You have to implement a custom converter.
As you can see you have to write a class just to get this done. It's of course easier to use country.getValue() in the viewmodel.
Another trick could be a command in the onSelect event, where you pass the value as parameter.

Greetz chill.

link publish delete flag offensive edit


hehe just read your answer, thanks for helping out

cor3000 ( 2014-07-24 03:07:56 +0800 )edit

Always welcome

chillworld ( 2014-07-24 06:52:14 +0800 )edit
Your answer
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

1 follower



Asked: 2014-07-23 06:25:59 +0800

Seen: 106 times

Last updated: Jul 23 '14

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