如何在Vaadin 8网格编辑器中编辑带有侦听器的两个组合框

时间:2018-04-05 05:01:26

标签: java vaadin vaadin8 vaadin-grid

我的可编辑ComboBoxes中有两个Grid,其中第二个ComboBox基于第一个ComboBox<String> makeComboBox = new ComboBox<String>(); ComboBox<String> modelComboBox = new ComboBox<String>(); 。例如,您将拥有Car Make,然后是Car Model。当你更改make组合框时,模型组合框会相应地改变。

考虑到这一点,我有:

grid.addColumn(CarRental::getPerson)
    .setEditorBinding(binder.forField(personComboxBox).bind(CarRental::getPerson, CarRental::setPerson));
grid.addColumn(CarRental::getMake)
    .setEditorBinding(binder.forField(makeComboxBox).bind(CarRental::getMake, CarRental::setMake));
grid.addColumn(CarRental::getModel)
    .setEditorBinding(binder.forField(modelComboxBox).bind(CarRental::getModel, CarRental::setModel));

具体做法是:

SelectionListener

这里的关键是如果改变了makeComboBox,我希望改变modelComboBox。换句话说,如果您选择本田,那么我希望模型ComboBox更改为Fit,Civic,Accord等。为此,我添加ValueChangeListener(但它也可能是makeComboBox.addSelectionListener(event -> { modelComboBox.clear(); modelComboBox.setItems(getModelsBasedOnMake(makeComboBox.getValue())); // Assuming someone has just edited the make value, // say changed from Toyota to Honda, then I want the model selected to be empty }); ,没关系,效果仍然相同)。

具体来说我有:

e added some logic to update the components on the

因为ComboBox可以是不同的,我for the Grid Editor OpenListener grid.getEditor().addOpenListener(open -> { ... CarRental selectedCarRental = (CarRental)event.getBean(); makeComboBox.setItems(makeList); modelComboBox.setItems(getModelsBasedOnMake(carRental.getMake())); }); 。具体来说,我做了:

RecyclerView

这里的问题是modelComBoxbox往往是未被选中的,因为如果你看它,就不能保证它会有什么价值,因为存在冲突。

我看了暂时禁用了selectionListener,但是所有的删除侦听器都已经被Vaadin 8弃用了。因此,如何设置网格以便能够在网格中编辑汽车品牌和模型?

1 个答案:

答案 0 :(得分:0)

我通过一个简单的例子尝试过它。对我来说没问题。什么是确切的问题? (我真的没有得到你的判决“因为如果你看它,就不能保证它会有什么价值,因为存在冲突。”)

@SpringUI
public class VaadinUI extends UI {

    @Override
    protected void init(VaadinRequest request) {
        VerticalLayout layout = new VerticalLayout();

        ComboBox<String> cmb1 = new ComboBox<>();
        ComboBox<String> cmb2 = new ComboBox<>();
        cmb1.setItems("1", "2", "3");
        cmb1.addSelectionListener(event -> {
            cmb2.clear();
            cmb2.setItems(getCmb2Content(event.getValue()));
        });

        Grid<MyBean> grid = new Grid<>();
        grid.setWidth("800px");
        grid.setHeightByRows(10);
        grid.addColumn(System::identityHashCode).setCaption("ID");
        grid.addColumn(MyBean::getProp1).setCaption("Prop 1")
                .setEditorBinding(grid.getEditor().getBinder().forField(cmb1).bind(MyBean::getProp1, MyBean::setProp1));
        grid.addColumn(MyBean::getProp2).setCaption("Prop 2")
                .setEditorBinding(grid.getEditor().getBinder().forField(cmb2).bind(MyBean::getProp2, MyBean::setProp2));
        grid.setItems(new MyBean(), new MyBean(), new MyBean());
        grid.getEditor().setEnabled(true);

        layout.addComponent(grid);

        setContent(layout);
    }

    private List<String> getCmb2Content(String cmb1Content) {
        return Arrays.asList(cmb1Content + "1", cmb1Content + "2", cmb1Content + "3");
    }

}

public static class MyBean {

    private String prop1;
    private String prop2;

    public String getProp1() {
        return prop1;
    }

    public void setProp1(String prop1) {
        this.prop1 = prop1;
    }

    public String getProp2() {
        return prop2;
    }

    public void setProp2(String prop2) {
        this.prop2 = prop2;
    }
}