代码之家  ›  专栏  ›  技术社区  ›  Nilambari

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

  •  0
  • Nilambari  · 技术社区  · 7 年前

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

    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

    1 回复  |  直到 7 年前
        1
  •  0
  •   Mika    7 年前

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