Vaadin-无法在组合框值更改时更新网格容器

Vaadin-Unable to update grid container on combobox value change

我正在使用 vaadin 7.7.7

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

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

我需要根据组合框选择更改更新同一行中的 property/cell

我的问题是,选择更改事件触发两次,一次是在单击组合框时触发,第二次是在更改选择值时触发。但是下一个单元格中的更新值仅在第一次反映在 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");}
   });

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

参考下方 link 图片

example image

即使字段获得了应该向用户显示的值,您也会收到值更改事件。为了获得指示用户已接受输入的事件,您应该使用字段组 (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);
    }
}