用于 MultiSelectComboBox 的 Vaadin 活页夹

问题描述 投票:0回答:1

有人可以帮我使用 Vaadin Binder 来实现这个“MultiSelectComboBox”吗?
MultiSelectComboBox 组件是否始终需要“Set”作为绑定方法?

我有这样的实体

@Entity(name = "recipes")
public class Recipe {

    // ...

    @Getter
    @Setter
    private List<Category> categories;

    // ...
}

我对 MultiSelectComboBox 的 Binder 实现

private final Binder<Recipe> binder = new Binder<>(Recipe.class);
private final MultiSelectComboBox<Category> categoryMultiselect = new MultiSelectComboBox<>("Categories");

// ...

private void initBinder() {
    binder.forField(categoryMultiselect)
            .asRequired("Please fill this field")
            .withValidator(s -> !s.isEmpty(), "Please select at least one category")
            .withValidator(s -> s.size() <= 5, "Please select up to 5 categories")
            .bind(Recipe::getCategories, Recipe::setCategories);
}

抛出

Incompatible types: Set<Category> is not convertible to List<Category>
,因为 MultiSelectComboBox 返回
Set
并且
Recipe::getCategories
返回
List<Category>

有什么想法可以避免这种情况吗?
PS:我真的不想将我的

List<Category>
中的
Set<Category>
更改为
Recipe.class

java binding vaadin vaadin-flow incompatibility
1个回答
0
投票

是的,它总是需要一个

Set
,因为这是其签名的一部分。这 意味着要选择的项目对于选择来说是唯一的,并且它们 是无序的 - 两者都是
List
无法承诺的 - 是否 这实际上对于尚不清楚的用例很重要。

因此,要么切换到另一个组件,允许订购和 重复项——或者在 UI 数据中的

Set
List
之间采用 类或活页夹(确保您的类别是一个好公民) a
Set
(给它等于和哈希码)并处理你的订单
List
通过某种方式(例如,为类别添加优先级))。

© www.soinside.com 2019 - 2024. All rights reserved.