2017-10-11 64 views
0

我使用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");} 
    }); 

需要更新在編輯模式組合框中變化中的單位列(保存之前)

請參考以下鏈接圖像

example image

回答

0

您將獲得價值變動事件,甚至當字段獲取應顯示給用戶的值。爲了得到表明用戶已接受輸入的事件,應使用字段組(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); 
    } 
} 
+0

但addComitHandler觸發保存點擊。我需要在保存之前根據組合框更改值更新其他單元格(在編輯模式下)。參考問題中的圖片。 – Nilambari

+0

謝謝..這幫了我:) – Nilambari