【问题标题】:Vaadin 8 - How to bind items of RadioButtonGroup?Vaadin 8 - 如何绑定 RadioButtonGroup 的项目?
【发布时间】:2021-03-03 02:41:30
【问题描述】:

我正在制作一个包含许多 TextField、ComboBox 和 CheckBox 的视图,它们的值由单个 Binder 处理。那里没问题。

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

问题#1:没有简单的 RadioButton 的组件(就像 CheckBox 一样),我只能找到 RadioButtonGroup。所以我想我将不得不与那个合作。

问题 #2:在Vaadin Docs 中明确表示:

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

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

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

这里有一些代码来演示我想要完成的事情:

// 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));

【问题讨论】:

    标签: java vaadin vaadin8


    【解决方案1】:

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

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

    public enum RadioButtonValue {
       foo, bar, foobar;
    }
    

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

    然后像这样上课:

    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,那么我认为最简单的方法是将上述内容更改一下:

    1. 根本不绑定无线电组。
    2. 实现ValueChangeListener,根据选定的枚举在 bean 中设置相应的布尔值。

    【讨论】:

    • Radio button [Groups] are typically used to assign value to a single attribute - 这就是我缺少的信息。你的枚举方法看起来很有希望,我会试一试。感谢您还提供了 ValueChangeListener 的替代方案。
    猜你喜欢
    • 2018-08-17
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-04
    相关资源
    最近更新 更多