【问题标题】:Vaadin update combobox values in a gridVaadin 更新网格中的组合框值
【发布时间】:2016-07-04 18:13:14
【问题描述】:

我有一个 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(),但也没有任何变化

我错过了什么?

【问题讨论】:

  • 我认为您的转换器方法convertToModel 没有意义。它在写入“id”列时被调用。它应该将A.getName() 映射回模型。但是,这不应该影响您的问题的行为。也许你有一个错字。您将容器数据源设置为 teamBox 而不是 theBox?
  • 关于convertToModel - 它似乎没有任何东西在那里工作,而且它似乎根本没有被调用(?)。是的,有一个错字。 theBoxCombobox,我在此更新项目。
  • 当我在更新方法中添加theBox.getContainerDataSource().getItemIds().forEach(System.out::println); 时,theBox 具有正确的对象,只有 UI 没有得到更新
  • 1 刚刚尝试在本地添加、删除、清除对我来说完美无缺,即使在编辑器处于活动状态时也是如此。一开始我唯一不知道的是,当我添加一个项目时,它是不可见的,因为它位于最后,所以我不得不滚动组合框。你能在 github 或类似网站上分享SSCCE 吗? 2 您可以将for (A each : as) {theBox.setItemCaption(a.getId(), a.getName()); 替换为theBox.setItemCaptionPropertyId("name");

标签: java web-applications combobox vaadin


【解决方案1】:

我找到了答案。

我的更新方法是这样的

public void update() {
   updateCombobox();
} 

现在是这个样子

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-15
    • 1970-01-01
    相关资源
    最近更新 更多