Vaadin Flow 24 – 选择或自定义返回值?

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

Vaadin Flow 24 - Select or Combox Custom Return Value?

问题

我正在使用 Vaadin 24 - 并且想要一个可选择的项目列表(无论是 Select 还是 Combobox 组件) - 它将允许不同的显示和返回值。

例如 - 给定这个数据集:

ID	姓名
1	Joe
2	Sarah
3	Bill
4	Jennifer

我想让组件显示姓名,但返回ID。

在普通的HTML中,我会做类似于:

<select>
	<option name="person" value="1">Joe</option>
	<option name="person" value="2">Sarah</option>
	<option name="person" value="3">Bill</option>
	<option name="person" value="4">Jennifer</option>
</select>

在 Vaadin 中有实现这个的方法吗?

英文:

I'm using Vaadin 24 - and would like to have a selectable list of items (either Select or Combobox Component) - that will allow a different display and return value.

For example - given this dataset:

ID	NAME
1	Joe
2	Sarah
3	Bill
4	Jennifer

I want the component to display the NAME, but return the ID.

In plain HTML I would do something like:

&lt;select&gt;
	&lt;option name=&quot;person&quot; value=&quot;1&quot;&gt;Joe&lt;/option&gt;
	&lt;option name=&quot;person&quot; value=&quot;2&quot;&gt;Sarah&lt;/option&gt;
	&lt;option name=&quot;person&quot; value=&quot;3&quot;&gt;Bill&lt;/option&gt;
	&lt;option name=&quot;person&quot; value=&quot;4&quot;&gt;Jennifer&lt;/option&gt;
&lt;/select&gt;

Is there a way to do this in Vaadin?

答案1

得分: 0

我假设您在客户端使用Web组件,而不是Flow框架,因为这两个组件的Java API完全处理对象,而不是特定的值。

因此,在客户端上,vaadin-selectvaadin-combo-box组件的工作方式与您描述的方式完全相同:通过items属性提供给它们一组JS对象,并指定这些对象中哪个属性将显示为它们的标签(item-label-path),以及哪个属性将用作它们的值(item-value-path),如Combo Box页面上最顶部的TypeScript示例所示。

英文:

I'm assuming you're using the web components client-side, rather than the Flow framework, as the Java API of both components deals entirely in objects rather than specific values.

So, on the client-side, both the vaadin-select and vaadin-combo-box components work precisely as you describe: you give them a collection of JS objects through the items property, and specify which property in those objects is displayed as their labels (item-label-path) and which property is to be used as their values (item-value-path), as shown in the topmost TypeScript sample on the Combo Box page.

答案2

得分: 0

Koobie出马 - comboBox有两种方法:
setItems()返回值,
setItemLabelGenerator()显示项。

从文档中:

ComboBox<Country> comboBox = new ComboBox<>();
comboBox.setItems(DataService.getCountries());
comboBox.setItemLabelGenerator(Country::getName);
add(comboBox);
英文:

Koobie to the rescue - there are two methods for the comboBox:
setItems() which is the return value and
setItemLabelGenerator() which is the display item.

From the docs:

ComboBox&lt;Country&gt; comboBox = new ComboBox&lt;&gt;(&quot;Country&quot;);
comboBox.setItems(DataService.getCountries());
comboBox.setItemLabelGenerator(Country::getName);
add(comboBox);

huangapple
  • 本文由 发表于 2023年7月28日 01:09:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76782048.html
匿名

发表评论

匿名网友

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

确定