3

在我使用 springboot 的 vaadin flow 项目中,我遇到了一个问题,即无法绑定带有组合框的枚举值。我在下面提供了我的代码。这里有人可以帮助我吗?

组合框实例化:

private ComboBox<Country> nationality = new CompoBox<>("Nationality");

绑定代码:

binder.forField(nationality)
    .bind(Coach::getNationality,Coach:setNationality);
4

1 回答 1

7

我想你错过了setItems电话。这是一个基于Vaadin Flow 的项目库的示例

package com.vaadin.starter.skeleton;

import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.combobox.ComboBox;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.data.binder.Binder;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.PWA;

/**
 * The main view contains a button and a click listener.
 */
@Route("")
@PWA(name = "Project Base for Vaadin Flow", shortName = "Project Base")
public class MainView extends VerticalLayout {

    public MainView() {

        ComboBox<ENUM> comboBox = new ComboBox<ENUM>("Number");
        comboBox.setItems(ENUM.values());
        Binder<Bean> binder = new Binder<>();
        binder.setBean(new Bean());
        binder.bind(comboBox,Bean::getField,Bean::setField);
        Button button = new Button("Check value",
                        e-> Notification.show("Value in bean is " + binder.getBean().getField()));
        add(button,comboBox);
    }

    public enum ENUM {
        ONE,TWO,TREE
    }

    public static class Bean {
        private ENUM field;

        public ENUM getField() {

            return field;
        }

        public void setField(ENUM field) {

            this.field = field;
        }
    }
}

于 2019-03-16T08:14:10.683 回答