繁体   English   中英

绑定组合框vaadin 8

[英]Binding a combo box vaadin 8

我正在尝试将vaadin 7代码转换为vaadin 8代码,而不是使用BeanFieldGroup vaadin 8 docs而是使用Binder将表单字段绑定到类。 这似乎不适用于组合框。

我一直在寻找一种使用转换器的方法,该方法似乎无法用于组合框。 如结合数据来形成vaadin文档中的位置

对于一个领域,转换器的工作原理是:

binder.forField(age).withConverter(
                    new 
StringToIntegerConverter("Must enter a number")).bind(
                    Student::getAge, 
Student::setAge);

但是对于组合框,我不确定这将如何工作。

ComboBox<String> gender = new ComboBox<String>("Gender");

Binder binder = new Binder<Student>(Student.class);

binder.bind(gender, Student::getGender, Student::setGender);

我知道这是行不通的,没有一种方法可以为组合框编写转换器,或者应该完全使用另一种方法。

您在评论中提到, Student对象中的“ gender字段实际上是一个枚举,而不是字符串。

您的错误是您使用字符串类型而不是性别枚举定义了ComboBox。

假设您的性别枚举类别称为Gender ,这将起作用:

ComboBox<Gender> gender = new ComboBox<Gender>("Gender");
Binder binder = new Binder<Student>(Student.class);
binder.bind(gender, Student::getGender, Student::setGender);

您可以向ComboBox添加ItemLabelGenerator来定义应如何显示Gender枚举。 默认情况下,它将使用该类的toString() 但是您可以根据需要使用它来构建Vaadin组件。 请参阅文档中的操作 )。

我发现在vaadin 8中使用bindInstanceFields将表单数据绑定到类。

Binder binder = new Binder<Student>(Student.class);

binder.bindInstanceFields(this);
binder.readBean(student);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM