首页 文章

Vaadin更新网格中的组合框值

提问于
浏览
0

我有一个Vaadin(7.6.7)网格,在编辑器模式下显示一个组合框 . 另一个动作可以修改组合框的项目,但网格中的组合框不会自行更新 .

public void setComboBoxAsEditor(Grid grid) {
  grid.getColumn("id").setEditorField(theBox).setConverter(new Converter<String, String>() {
    @Override
    public String convertToModel(String value, Class<? extends String> targetType, Locale locale) throws ConversionException {
        return value; // not sure for what this is required
    }

    @Override
    public String convertToPresentation(String value, Class<? extends String> targetType, Locale locale) throws ConversionException {
        // don't show the id, but the name
        A a = endpoint.getA(value);
        return a.getName();
    }

    @Override
    public Class<String> getModelType() {
        return String.class;
    }

    @Override
    public Class<String> getPresentationType() {
        return String.class;
    }
 });
}

theBox 是之前填充的Combobox - 初始显示正确(它包含 A 类型的对象)

现在另一个动作操纵 A 类型的对象,例如删除一个对象 .

我现在如何更新网格中的组合框?

我试过了

public void upateCombobox() {
 theBox.removeAllItems();
 List<A> as = endpoint.getAs();
 theBox.setContainerDataSource(new BeanItemContainer<>(String.class, as.stream().map(A::getIdent).collect(Collectors.toList())));
 for (A each : as) {
    theBox.setItemCaption(a.getId(), a.getName());
 }
}

之前调用,但在编辑网格时, theBox 仍显示旧值 .

我试着打电话给 grid.getColumn("id").getEditorField().markAsDirty() ,但也没有变化

我错过了什么?

1 回答

  • 0

    我找到了答案 .

    我的更新方法看起来像这样

    public void update() {
       updateCombobox();
    }
    

    现在看起来像这样

    public void update() {
        createCombobox(); // this creates a new reference to theBox
        setComboboxAsEditor(); 
    }
    

    因此,我创建 theBox 的新实例并将其填入 updateCombobox 中的数据,然后将其设置为列的新编辑器字段 .

    不确定它的正确性,但它确实有效

相关问题