0

Databinding radiogroup problem

asked 2009-05-13 16:51:27 +0800

lramellavotta gravatar image lramellavotta flag of Italy
200 1 8

Hi, I have a form with textbox e radiogroup. In this form I insert/delete/update records from MySQL
The table structure of MySQL is:
Utente varchar(15)
...
Utente_attivo bit <--- for TRUE/FALSE

Any user inserted could be ACTIVE or INACTIVE (Utente_attivo).

I can't correctly set my radiogroup in the form ... Any idea? I can't use "value=false" ???
n.b. the textbox txtAttivo is only for "debug" mode....

thanks


User/Active user
<hbox>
<textbox width="90%" value="@{win.current.Utente}" maxlength="15" />
<textbox id="txtAttivo" width="90%" value="@{win.current.Utente_attivo}" maxlength="1" />
<radiogroup id="chkUtente_attivo" selectedItem="@{win.current.Utente_attivo}">
<radio id="chkAttivo" value="true" label="Attivo" />
<radio id="chkNonAttivo" value="false" label="Non attivo" />
</radiogroup>
</hbox>

delete flag offensive retag edit

8 Replies

Sort by ยป oldest newest

answered 2009-05-15 13:38:27 +0800

lramellavotta gravatar image lramellavotta flag of Italy
200 1 8

@Robert
It works! You are the best! Thank you

link publish delete flag offensive edit

answered 2009-05-15 08:58:14 +0800

robertpic71 gravatar image robertpic71
1275 1

Here it is:

my test.zul:

<?page title="Index" contentType="text/html;charset=UTF-8"?>
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" ?>
<zk>
    <zscript>
    	boolean test = true;
    </zscript>
	<window id="testWin" title="Index" border="normal">
	    <vbox>
	    <radiogroup selectedItem="@{test, converter=webtests.binder.RadiogroupBooleanConverter}">
	    <radio label="Yes" value="true"/>
	    <radio label="No" value="false"/>
	    </radiogroup>
		<label id="hide" value="@{test}"/>
		</vbox>
	</window>
</zk>

The converter:

package webtests.binder;

/* 

{{IS_NOTE
   Purpose:
          Description:
          History:
       Mon Mar 12 11:05:43     2007, Created by Henri
}}IS_NOTE

Copyright (C) 2007 Potix Corporation. All Rights Reserved.

{{IS_RIGHT
}}IS_RIGHT
*/

import java.util.Iterator;

import org.zkoss.zk.ui.Component;
import org.zkoss.zkplus.databind.TypeConverter;
import org.zkoss.zul.Radio;
import org.zkoss.zul.Radiogroup;

/**
* Convert Radiogroup selected item to radio value and vice versa.
* modified for boolean
*
* zk's RadiogroupSelectedListItemConverter modified for boolean
* @author Henri
*/
public class RadiogroupBooleanConverter implements TypeConverter, java.io.Serializable {
        private static final long serialVersionUID = -7692059172828069699L;

   public Object coerceToUi(Object val, Component comp) { //load
       if (val != null && val instanceof Boolean) {
           Boolean bval = (Boolean) val;
           //iterate to find the selected radio via the value
           for (Iterator it = comp.getChildren().iterator(); it.hasNext();) {
               final Component child = (Component)it.next();
               if (child instanceof Radio) {
                   if (bval.toString().equalsIgnoreCase(((Radio)child).getValue())) {
                       return child;
                   }
               } else if (!(child instanceof Radiogroup)) { //skip nested radiogroup
                   //bug 2464484
                   final Object value = coerceToUi(val, child); //recursive
                   if (value != null) {
                       return value;
                   }
               }
           }
       }
       return null;
   }

   public Object coerceToBean(Object val, Component comp) { //save
               return val != null ? Boolean.parseBoolean(((Radio)val).getValue()) : null;
   }
} 

/Robert

link publish delete flag offensive edit

answered 2009-05-15 07:22:47 +0800

lramellavotta gravatar image lramellavotta flag of Italy
200 1 8

Hi Robert, are you news for my problem?
Thanks

link publish delete flag offensive edit

answered 2009-05-14 08:55:10 +0800

lramellavotta gravatar image lramellavotta flag of Italy
200 1 8

Ok, thanks Roberts..

link publish delete flag offensive edit

answered 2009-05-14 08:07:31 +0800

robertpic71 gravatar image robertpic71
1275 1

I have some meetings now. I'll post my converter later.

/Robert

link publish delete flag offensive edit

answered 2009-05-14 07:57:40 +0800

lramellavotta gravatar image lramellavotta flag of Italy
200 1 8

I tried to use the converter RadiogroupSelectedItemConverter but my form return this error:

Cause: Not a child: <Radio null>
org.zkoss.zk.ui.UiException: Not a child: <Radio null>
at org.zkoss.zul.Radiogroup.setSelectedItem(Radiogroup.java:161)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.zkoss.zk.ui.metainfo.Property.assign0(Property.java:260)
at org.zkoss.zk.ui.metainfo.Property.assign(Property.java:175)
at org.zkoss.zk.ui.metainfo.ComponentInfo.applyProperties(ComponentInfo.java:769)
at org.zkoss.zk.ui.impl.AbstractUiFactory.newComponent(AbstractUiFactory.java:95)
...


Code in .zul form:


<radiogroup id="chkUtente_attivo" selectedItem="@{win.current.Utente_attivo}, converter=RadiogroupSelectedItemConverter()"
onCheck="onCheckUtente_attivo(self, self.selectedItem.id)">
<radio id="chkAttivo" value = "1" label="Attivo" />
<radio id="chkNonAttivo" value = "0" label="Non attivo" />
</radiogroup>

Sorry but I have recently started to use zk and often I am in trouble ...

link publish delete flag offensive edit

answered 2009-05-13 20:57:16 +0800

lramellavotta gravatar image lramellavotta flag of Italy
200 1 8

Thanks Robert. I will read tomorrow your link...

link publish delete flag offensive edit

answered 2009-05-13 17:25:51 +0800

robertpic71 gravatar image robertpic71
1275 1

It's possible with converter. Check this thread.

Post if you need more help.

/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: 2009-05-13 16:51:27 +0800

Seen: 862 times

Last updated: May 15 '09

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