Vaadin更新网格中的组合框值

时间:2016-07-04 18:13:15

标签: java web-applications combobox vaadin

我有一个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 :(得分:0)

我找到了答案。

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

public void update() {
   updateCombobox();
} 

现在看起来像这样

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

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

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

相关问题