Vaadin-无法在组合框的值变化更新网格容器
问题描述:
我使用vaadin 7.7.7Vaadin-无法在组合框的值变化更新网格容器
在网格我有在列 之一的组合框作为编辑项目作为
grid.addColumn("columnProperty").setEditorField(combobox);
我需要根据组合框选择更改来更新同一行中的属性/单元格
我的问题是,选择更改事件触发两次,一次单击组合框时,第二次更改选择值。但是下一个单元格中的更新值仅在第一次用户界面时才反映出来。 下面是编写的代码。任何解决方案
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");}
});
需要更新在编辑模式组合框中变化中的单位列(保存之前)
请参考以下链接图像
答
您将获得价值变动事件,甚至当字段获取应显示给用户的值。为了得到表明用户已接受输入的事件,应使用字段组(setEditorFieldGroup)。
从书Vaadin 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");
}
});
编辑
我假设你想连接参数和单元组合框。我会这样做,这种价值变化lister
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触发保存点击。我需要在保存之前根据组合框更改值更新其他单元格(在编辑模式下)。参考问题中的图片。 – Nilambari
谢谢..这帮了我:) – Nilambari