Vaadin 8 - 如何绑定 RadioButtonGroup 的项目? [英] Vaadin 8 - How to bind items of RadioButtonGroup?

查看:29
本文介绍了Vaadin 8 - 如何绑定 RadioButtonGroup 的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个包含许多文本字段、组合框和复选框的视图,其中它们的值由单个 Binder 处理.没有问题.

I am making a view that has many TextFields and ComboBoxes and CheckBoxes, where their values are handled by a single Binder. No problem there.

但是:现在我想在视图中添加 3 个 RadioButton,这些值应该来自同一个 Binder.每个 RadioButton 绑定到不同的布尔字段,这些字段中只有 1 个可以同时为真(RadioBoxes 的完美要求).

BUT: now I want to add 3 RadioButtons to the view, the values should come from the same Binder. Each RadioButton is bound to a different boolean field, only 1 of those fields can be true at the same time (perfect requirement for RadioBoxes).

问题 #1:没有用于简单 RadioButton 的组件(就像 CheckBox 一样),我只能找到 RadioButtonGroup.所以我想我将不得不和那个人一起工作.

Problem #1: There is no Component for a simple RadioButton (like there is for CheckBox), i can only find RadioButtonGroup. So i guess I will have to work with that one.

问题 #2:在 Vaadin Docs 它特别指出:

CheckBoxGroup 组件的优点在于,由于它维护了各个复选框对象,因此您可以轻松获取当前选定项的数组,并且您可以轻松更改单个复选框的外观组件并将其与 Binder 一起使用.

The advantages of the CheckBoxGroup component are that as it maintains the individual check box objects, you can get an array of the currently selected items easily, and that you can easily change the appearance of a single component and use it with a Binder.

但我找不到绑定 RadioButtonGroup 项目的方法,也找不到任何提及它的地方.

But I can't find a way to bind the items of RadioButtonGroup, nor can I find any mention of it anywhere.

有没有办法在 RadioButtonGroup 中绑定单个项目?
如果没有,那么我担心我将不得不使用 CheckBoxes,而 RadioButtons 是一种更合乎逻辑的方法.

Is there a way to bind single Items in a RadioButtonGroup?
If not, then I fear I will have to use CheckBoxes where RadioButtons would be a more logical approach.

以下是一些代码来演示我要完成的任务:

Here is some code to demonstrate what I am trying to accomplish:

// FooBar Class
private boolean foo = true;
private boolean bar = false;
private boolean fooBar = false;
// constructor, getters and setters

<小时>

// My View
Binder<FooBar> binder = new Binder<>();
binder.setBean(new FooBar());

// this CheckBox works perfectly fine like this
Checkbox cb = new CheckBox();
cb.setCaption("Foo");
binder.forItem(cb)
    .bind(f -> f.isFoo, (f, b) -> f.setFoo(b));

// this part is where I'm confused
RadioButtonGroup<String> rbg = new RadioButtonGroup<>();
rbg.setItems("Foo", "Bar", "FooBar");
// how can i bind each RadioButton to different fields of my FooBar Bean?
// because getItem() does not exist
binder.forItem(rbg.getItem(0)).bind(f -> f.isFoo,    (f, b) -> f.setFoo(b));
binder.forItem(rbg.getItem(1)).bind(f -> f.isBar,    (f, b) -> f.setBar(b));
binder.forItem(rbg.getItem(2)).bind(f -> f.isFooBar, (f, b) -> f.setFooBar(b));

推荐答案

我建议考虑一些不同的方法.单选按钮通常用于为单个属性赋值 - RadioButtonGroup 是单个表单字段 - 而不是属性或字段列表,所以我想这就是您找不到直接解决方案的原因.

I suggest to consider a bit different approach. Radio buttons are typically used to assign value to a single attribute - RadioButtonGroup is a single form field - not to a list of attributes or fields so I guess that is why you can not find a straightforward solution.

如果可能,将您的三个 boolean 更改为 enum,例如:

If possible change your three booleans to an enum like:

public enum RadioButtonValue {
   foo, bar, foobar;
}

这应该提供兼容的功能,因为您希望一次只限制三个布尔值之一为真.

This should provide compatible functionality since you wanted to restrict only one of the three booleans to be true at a time.

然后上课:

public class RadioButtonBean {
   @Getter @Setter // Lombok
   private RadioButtonValue radioButtonValue;
   // replaces boolean foo, bar, foobar;
}

允许您轻松进行绑定:

RadioButtonGroup<RadioButtonValue> radioGroup = new RadioButtonGroup<>();
radioGroup.setCaption("Radiobutton group");
// populate with enum values as title or use setItemCaptionGenerator(...);
radioGroup.setDataProvider(
    new ListDataProvider<RadioButtonValue>( 
        Arrays.asList( RadioButtonValue.values() )
    )
);

final RadioButtonBean bean = new RadioButtonBean();
Binder<RadioButtonBean> binder = new Binder<>();
binder.setBean(bean);
binder.forField(radioGroup).bind(RadioButtonBean::getRadioButtonValue,
        RadioButtonBean::setRadioButtonValue );
// uncomment for testing it
//      radioGroup.addValueChangeListener( vc -> {
//         Notification.show("bean enum value: "+ bean.getRadioButtonValue() );
//      });

如果不可能将 booleans 更改为 enum 那么我认为最简单的方法是对上面的内容进行一些更改:

If it is impossible to change the booleans to enum then I think the easiest way to is to change the above a bit:

  1. 完全不要绑定无线电组.
  2. 实现 ValueChangeListener,根据选定的枚举在 bean 中设置相应的布尔值.
  1. Do not bind the radio group at all.
  2. Implement ValueChangeListener that sets corresponding boolean in the bean based on the selected enum.

这篇关于Vaadin 8 - 如何绑定 RadioButtonGroup 的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆