0

How do I bind data to a radiogroup?

asked 2008-12-01 17:06:27 +0800

OverFlo123 gravatar image 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???

<radiogroup id="mything">
<radio label="123"/>
<radio label="456"/>
<radio label="789"/>
<radio label="000"/>
</radiogroup>

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

any ideas?

help apreciated.

kind regards

Florian

delete flag offensive retag edit

8 Replies

Sort by » oldest newest

answered 2008-12-01 18:18:07 +0800

hkn gravatar image hkn
246 3

Hello !

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

I use (with java) radiogroup.setSelectedIndex( radioNumberToSet );
or if you assign each radio an id: radioToSet.setChecked( true|false );

so .....

/Horst

link publish delete flag offensive edit

answered 2008-12-02 16:14:17 +0800

OverFlo123 gravatar image 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

link publish delete flag offensive edit

answered 2008-12-02 17:51:22 +0800

robertpic71 gravatar image 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="2" label="Not so much."/>
    <radio value="3" label="I'm indifferent."/>
    <radio value="4" label="I'm pretty neat."/>
    <radio value="5" label="I'm totally in love with me."/>
</radiogroup>  

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}"/>
                               </radiogroup>      
				<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

link publish delete flag offensive edit

answered 2008-12-02 20:16:08 +0800

hkn gravatar image 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

link publish delete flag offensive edit

answered 2008-12-03 00:44:29 +0800

robertpic71 gravatar image 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

3.) Create/extend your own Radiogroup
Add a simple setModel(List list) and/or setModel(List list, Render render) can help.
setModel(.. List or your own Radiogroupmodel)
- 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

link publish delete flag offensive edit

answered 2008-12-03 07:27:39 +0800

hkn gravatar image 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

link publish delete flag offensive edit

answered 2008-12-03 08:06:37 +0800

OverFlo123 gravatar image OverFlo123
63 5

Thanks for the

<radiogroup id="rating" selectedItem="@{controller.PersonBean.rating}">

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

thanks

Florina

link publish delete flag offensive edit

answered 2008-12-03 11:52:28 +0800

robertpic71 gravatar image 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?.
You can see the Radiogroup in the lang-addon.xml.

/Robert

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: 2008-12-01 17:06:27 +0800

Seen: 1,255 times

Last updated: Dec 03 '08

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