【问题标题】:Vaadin-Unable to update grid container on combobox value changeVaadin - 无法在组合框值更改时更新网格容器
【发布时间】:2017-10-11 09:54:09
【问题描述】:

我正在使用 vaadin 7.7.7

在网格中,我在其中一列中有一个组合框作为编辑项 作为

grid.addColumn("columnProperty").setEditorField(combobox);

我需要根据组合框选择更改更新同一行中的属性/单元格

我的问题是,选择更改事件会触发两次,一次是在单击组合框时触发,第二次是在更改选择值时触发。但是下一个单元格中的更新值仅在第一次反映在 UI 上。 下面是编写的代码。有什么解决办法吗?

Combobox.addValueChangeListener(new ValueChangeListener()
@Override
public void valueChange(ValueChangeEvent event) {

// below line works only first time when the combobox is clicked,but i want 
//it when the item in the combobox is changed

gridContainer.getContainerProperty(editedRow,"editedColumProperty").setValue("ValueTobeUpdated");}
   });

需要在编辑模式下更新组合框更改的单位列(保存前)

图片参考以下链接

example image

【问题讨论】:

    标签: combobox grid containers vaadin selection


    【解决方案1】:

    即使字段获得了应该向用户显示的值,您也会收到值更改事件。为了获得表明用户已接受输入的事件,您应该使用字段组 (setEditorFieldGroup)。

    来自瓦丁之书example for grid editing

    grid.getColumn("name").setEditorField(nameEditor);
    
    FieldGroup fieldGroup = new FieldGroup();
    grid.setEditorFieldGroup(fieldGroup);
    fieldGroup.addCommitHandler(new CommitHandler() {
        private static final long serialVersionUID = -8378742499490422335L;
    
        @Override
        public void preCommit(CommitEvent commitEvent)
               throws CommitException {
        }
    
        @Override
        public void postCommit(CommitEvent commitEvent)
               throws CommitException {
            Notification.show("Saved successfully");
        }
    });
    

    编辑

    我假设您想要连接参数和单位组合框。我会使用这种值更改列表器来做到这一点

    BeanItemContainer container = new BeanItemContainer<>(
            Measurement.class,
            measurements);
    Grid grid = new Grid(container);
    grid.setEditorEnabled(true);
    ComboBox parameterComboBox = new ComboBox();
    ComboBox unitComboBox = new ComboBox();
    parameterComboBox.addItems(Parameter.Pressure, Parameter.Temperature, Parameter.Time);
    parameterComboBox.addValueChangeListener(v -> setUnits(parameterComboBox, unitComboBox));
    grid.getColumn("parameter").setEditorField(parameterComboBox);
    grid.getColumn("unit").setEditorField(unitComboBox);
    

    单位可以这样更新。如果您替换组合框中的可用项目,我认为您需要保留当前值并将其重新设置。

    private void setUnits(ComboBox parameterComboBox, ComboBox unitComboBox) {
        Object currentValue = unitComboBox.getValue();
        List<String> units = unitsForParameter(parameterComboBox.getValue());
        unitComboBox.removeAllItems();
        unitComboBox.addItems(units);
        if (units.contains(currentValue)) {
            unitComboBox.setValue(currentValue);
        } else {
            unitComboBox.setValue(null);
        }
    }
    
    private List<String> unitsForParameter(Object value) {
        if (value == null) {
            return Collections.emptyList();
        } else if (value == Parameter.Pressure) {
            return asList("Pascal", "Bar");
        } else if (value == Parameter.Temperature) {
            return asList("Celcius", "Kelvin");
        } else if (value == Parameter.Time) {
            return asList("Second", "Minute");
        } else {
            throw new IllegalArgumentException("Unhandled value: " + value);
        }
    }
    

    【讨论】:

    • 但 addComitHandler 在保存点击时触发。我需要在保存之前根据组合框更改值更新其他单元格(在编辑模式下)。参考问题中的图片。
    猜你喜欢
    • 1970-01-01
    • 2011-02-15
    • 1970-01-01
    • 1970-01-01
    • 2014-12-15
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 2013-02-15
    相关资源
    最近更新 更多