How do I bind data to a radiogroup?

OverFlo123
63 5

I can bind data to textbox - easy:

<textbox height="100px" width="100%" multiline="true" value="@{controller.textboxdata}" />

but how do I bind data to a radiogroup???

there seems to be no value or model or anything...

any ideas?

help apreciated.

kind regards

Florian

delete retag edit

8 Replies

hkn
246 3

Hello !

yes, there is no model and I miss radioGroup.setModel( model ) similar to listboxes too.

so .....

/Horst

OverFlo123
63 5

right does not work with databinding.

I just use "getSelectedItem().getLabel()"
(dont use getValue - doesnt return anything!)

It would be nice to know which Components support @databinding and which don´t!

anyway - have a nice day

robertpic71
1275 1

I cannot catch your problem. You don't need do any javacode for binding, just

<radiogroup id="rating" selectedItem="@{controller.PersonBean.rating}">
<radio value="1" label="Actually, I hate me."/>
<radio value="5" label="I'm totally in love with me."/>


Here is the complete example.

The value rating from PersonBean is set to "1", "2", "3", "4", "5".
However, there are some limits:
- since there is no model, you could annotate the radio-items
- Radio.value could only hold String - no Object
- for static radiogroups (content known at rendertime) you could use forEach="${List}" Of course, i would prefer an @bindingmodel and/or @forEach, but it is possible to bind radiogroup <?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" arg0="./mywin"?> <window id="mywin" title="Hibernate" border="normal"> <zscript> //init example data beans import org.zkoss.zkdemo.userguide.Person; List persons = new ArrayList(); persons.add(new Person("Balu", "Haben")); persons.add(new Person("Sadira", "Jobs")); persons.add(new Person("Dacey", "Obert")); persons.add(new Person("Gabby", "Taffy")); Person selected = persons.get(0); </zscript> <listbox model="@{persons}" selectedItem="@{selected}" rows="5"> <listitem self="@{each=person}" label="@{person.fullName}" /> </listbox> <grid> <columns> <column label="First Name" /> <column label="Last Name" /> <column label="Full Name" /> </columns> <rows> <row> <radiogroup selectedItem="@{selected.firstName}"> <radio forEach="${persons}" value="${each.firstName}" label="${each.firstName}"/>
<textbox value="@{selected.lastName}" />
<label value="@{selected.fullName}" />
</row>
</rows>
</grid>
</window>

This run's in the demo explorer.

You can see:
1.) there is no problem for fixed radio-values (maybe you require a converter if your value is not String)
2.) you can use dynamic values, since you know them, while the renderingphase

/Robert

hkn
246 3

Hello Robert,

first of all thank you for your detailed explanation!

My question is a little bit different: "what when the list of elements (radios) is dynamic?"
A listbox and its items are updated when the model changes. That is great!
Also it is quite similar for comboboxes, listboxes, trees, grids etc. In all cases
we have a model and a renderer, but I do not know something similar for radiogroups.

Kind regards
/Horst

robertpic71
1275 1

Hello Horst,

>> what when the list of elements (radios) is dynamic?
Yes, that is a problem. With forEach=\${} you could only create from a list - no modification, no changes after render.

>> but I do not know something similar for radiogroups
1.) Using a small listbox
<listbox rows="3">
<listitem label="option1"/>
<listitem label="option2"/>
<listitem label="option3"/>
</listbox>

2.) Post feature-request

Add a simple setModel(List list) and/or setModel(List list, Render render) can help.
- remove all childs (this = Radiogroup)
- create new childs from the list/model
Of course this simple resolution is limited, but should work when you have to fill your radiogroup from javacode.

/Robert

hkn
246 3

Hello Robert,

Yes, you are quite right and I use your third approach. Nevertheless to me this should be part of the product to make the API more uniform.

I will post a feature request.

Kind Regards
/Horst

OverFlo123
63 5

Thanks for the

tip - thats what I was looking for
(where did you find that?)

thanks

Florina

robertpic71
1275 1

Hello Florina,

>> where did you find that?
Sorry, I don't know - i think it was a release note. I'm using ZK since 2.1 and @databinding since it was made..

Check also my other answer in Which Components support Databinding?.

/Robert

[hide preview]