Vaadin 下拉菜单/选择框/微调器最佳实践?

huangapple go评论82阅读模式
英文:

Vaadin DropDown/Select/Spinner best practice?

问题

我正在查看Vaadin组件,似乎Vaadin中的“favoured” Select组件不在索引上运行。

使用以下代码:

List<String> list = new ArrayList<>();
    list.add("A");
    list.add("B");
    list.add("A");
    
    Select<String> select = new Select<>();
    select.setItems(list);

因此,当我有以下列表时:

  1. A
  2. B
  3. A

并且我选择第三个选项,即A,它将显示为列表中的第一个选项,同样是A。是否有一个在索引值上运行的Vaadin组件或带有Java实现的库?或者是否已经有一种解决方法。

英文:

I am looking at the Vaadin components and it seems like the "favoured" Select-component in Vaadin does not run on indexing.

Using this code:

List&lt;String&gt; list = new ArrayList&lt;&gt;();
    list.add(&quot;A&quot;);
    list.add(&quot;B&quot;);
    list.add(&quot;A&quot;);
    
    Select&lt;String&gt; select = new Select&lt;&gt;();
    select.setItems(list);

So when I have the following list:

  1. A
  2. B
  3. A

And I choose the third option, A, it would appear as the first option, also A, in the list. Is there a Vaadin component or library with java implementation which runs on indexed values? or has there been a workaround.

答案1

得分: 2

用户如何区分这些相同的选项?

Select 组件依赖于 equalshashCode 方法来区分项目。在您的情况下,这两个字符串彼此相等,因此在组件的视角下它们是相同的。

因此,如果您对此有一个有效的用例,您将需要传递在用例中正确实现了 equals 的项目。

有几种方法可以做到这一点:制作一个自定义类,传入一个值映射,传入一个枚举等。在所有情况下,您可能都希望使用自定义的项目标签生成器。

使用映射,代码可能如下所示:

Map<Integer, String> values = new HashMap<>();
values.put(1, "A");
values.put(2, "B");
values.put(3, "A");

Select<Integer> select = new Select<>();
select.setItems(values.keySet());
select.setItemLabelGenerator(values::get);

add(select);

或者像 cfrick 建议的那样,使用枚举:

enum Option {
    FIRST_A("A"), B("B"), SECOND_A("A");

    private final String name;

    Option(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

...

Select<Option> select = new Select<>();
select.setItems(Option.values());
select.setItemLabelGenerator(Option::getName);

add(select);
英文:

How would the end user distinguish those identical options?

The Select component relies on the equals and hashCode methods to distinguish items. In your case, the two strings are equal to each other, and as such they are the same from the component's point of view.

So if you have a valid use case for this, you will have to pass items where equals is properly implemented for the use case.

There are several ways to do this: making a custom class, passing in a map of values, passing in an enum etc. In all cases you will probably want to use a custom item label generator.

With a map, it would look something like this:

Map&lt;Integer, String&gt; values = new HashMap&lt;&gt;();
values.put(1, &quot;A&quot;);
values.put(2, &quot;B&quot;);
values.put(3, &quot;A&quot;);

Select&lt;Integer&gt; select = new Select&lt;&gt;();
select.setItems(values.keySet());
select.setItemLabelGenerator(values::get);

add(select);

Or with an enum, as cfrick suggested:

enum Option {
    FIRST_A(&quot;A&quot;), B(&quot;B&quot;), SECOND_A(&quot;A&quot;);

    private final String name;

    Option(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

...

Select&lt;Option&gt; select = new Select&lt;&gt;();
select.setItems(Option.values());
select.setItemLabelGenerator(Option::getName);

add(select);

huangapple
  • 本文由 发表于 2020年9月28日 15:30:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64097725.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定