0

Binding of radio checked/selected is not working as expected

asked 2013-02-20 17:59:35 +0800

RetoH gravatar image RetoH flag of Switzerland
44 5

updated 2013-02-26 13:29:37 +0800

I have a radiogroup:

  <radiogroup onCheck="@command('optionSelected')">
    <radio label="Option 1" checked="@bind(vm.option1)" />
    <radio label="Option 2" checked="@bind(vm.option2)" />
  </radiogroup>

If I select an option, true is pushed down to the model, but the other option (deselected) is not pushing false down to the model. After clicking both options, both boolean properties in the model are set to true. The same problem exists if I bind to the "selected" attribute of radio.

What am I doing wrong?

Summary so far: It seems like the zk framework does not handle a binding of the 'checked' and the 'selected' property of the 'radio' component correctly. There are other options/workarounds.

Edit: Here's my view model to clarify what I would like to achieve.

public class TestVM
{
  private boolean m_option1 = false;
  private boolean m_option2 = false;

  @Command
  public void optionSelected()
  {
    if (isOption1())
    {
      // handle option 1
    }
    else if (isOption2())
    {
      // handle option 2
    }
    else
    {
      throw new IllegalStateException("unexpected");
    }
  }

  public boolean isOption1()
  {
    return m_option1;
  }

  public void setOption1(boolean option1)
  {
    m_option1 = option1;
  }

  public boolean isOption2()
  {
    return m_option2;
  }

  public void setOption2(boolean option2)
  {
    m_option2 = option2;
  }
}
delete flag offensive retag edit

Comments

For the radio element, which loses the selection, no check-event is sent. You have to monitor this status change itself. During the processing of the command optionSelected, you can reset vm.option1/2.

khcyt ( 2013-02-21 07:34:25 +0800 )edit

Too bad, from my point of view that is the job of the framework. So I guess there is no point in binding to the radio components checked property :-(. During the processing of the command 'optionSelected' I wanted to use vm.option1/2, not maintain them lol.

RetoH ( 2013-02-21 22:31:07 +0800 )edit

3 Answers

Sort by ยป oldest newest most voted
0

answered 2013-02-20 21:02:42 +0800

Matze2 gravatar image Matze2
773 7

Binding to the selectedItem of the radiogroup works for me:

<radiogroup selectedItem="@bind(vm.selection)">
  <radio label="Label1" value="value1"/>
  <radio label="Label2" value="value2"/>
</radiogroup>

Note that the corresponding VM property must be of type String. A commmand is not needed here.

link publish delete flag offensive edit

Comments

That works for me too. My question is about binding to the checked/selected property of the radio components.

RetoH ( 2013-02-21 22:27:41 +0800 )edit

Don't understand that. If you monitor vm.selection, you notice each "checked" state-change. Why do you need to monitor the radios directly?

Matze2 ( 2013-02-22 16:01:21 +0800 )edit

To clarify that a little bit more: a change in the radio group selection triggers a call to the corresponding setter method (here: setSelection()). You do not need a command here at all.

Matze2 ( 2013-02-22 16:04:10 +0800 )edit

The reason is that I didn't want to use strings for the radio values (although currently that's my working solution). I updated my question to make my intent more clear.

RetoH ( 2013-02-23 11:28:35 +0800 )edit
0

answered 2013-02-23 11:34:24 +0800

RetoH gravatar image RetoH flag of Switzerland
44 5

updated 2013-02-26 13:24:58 +0800

This is my current working solution that I would like to replace (I believe this is what Matze2 suggests.

ZUL:

<radiogroup selectedItem="@bind(vm.selectedOption)" onCheck="@command('optionSelected')">
  <radio label="Option 1" value="option1"/>
  <radio label="Option 2" value="option2"/>
</radiogroup>

And the model:

  public class TestVM
  {
    private String m_selectedOption = null;

    @Command
    public void optionSelected()
    {
      if ("option1".equals(m_selectedOption))
      {
        // handle option 1
      }
      else if ("option2".equals(m_selectedOption))
      {
        // handle option 2
      }
      else
      {
        throw new IllegalStateException("unexpected");
      }
    }

    public String getSelectedOption()
    {
      return m_selectedOption;
    }

    public void setSelectedOption(String selectedOption)
    {
      m_selectedOption = selectedOption;
    }
  }
link publish delete flag offensive edit

Comments

Sort of. You can use internally also an enum if you want and convert it on the fly in the getter (name()) and setter (valueOf). And I would merge the optionSelected() into setSelectedOption() method (no idea how you would call it otherwise). It's not the cleanest code, but short and understandable.

Matze2 ( 2013-02-26 00:29:05 +0800 )edit

Yes I also thought of an enum and described that solution in another answer.

RetoH ( 2013-02-26 13:27:55 +0800 )edit
0

answered 2013-02-23 12:08:08 +0800

RetoH gravatar image RetoH flag of Switzerland
44 5

And here's how it probably should be done (the clean solution requiring a bit more typing effort):

  <radiogroup model="@load(vm.options)" selectedItem="@bind(vm.selectedOption)" onCheck="@command('optionSelected')">
    <template name="model">
      <radio label="${each.label}"  />
    </template>
  </radiogroup>

And the model:

public class TestVM
{
  public enum Option
  {
    OPTION_1("Option 1"), //
    OPTION_2("Option 2"), //
    ;

    private final String m_label;

    private FilterOption(String label)
    {
      m_label = label;
    }

    public String getLabel()
    {
      return m_label;
    }
  }

  private Option m_selectedOption = FilterOption.OPTION_1;

  public List<Option> getOptions()
  {
    return Arrays.asList(Option.values());
  }

  @Command
  public void optionSelected()
  {
    switch(getSelectedOption())
    {
        // handle options
    }
  }
}
link publish delete flag offensive 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

Follow
1 follower

RSS

Stats

Asked: 2013-02-20 17:59:35 +0800

Seen: 122 times

Last updated: Feb 26 '13

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