Home > Back-end >  Vaadin - How to specify select option value?
Vaadin - How to specify select option value?

Time:01-31

I want to have option value as state code (AL, AK, AZ) and the label to be state name (Alabama, Alaska, Arizona) using Vaadin select component.

states.setItems(facade.stateService().findAllStates().stream().map(States::getName).collect(Collectors.toList()));
<option value="AL">Alabama</option>

How to generate the above option in vaadin? Thanks.

CodePudding user response:

You typically don't have to be concerned with the exact HTML representation when using Vaadin. The framework is automatically generating internal identifiers for all the items so that you can use the full object representation (e.g. States instances in your case) in the Java API.

You would then get an instance of States as the value of the select and also in various events.

Select<States> states = new Select<>();
states.setItems(facade.stateService().findAllStates());
states.setItemLabelGenerator(States::getName);
states.addValueChangeListener(event -> {
  Notification.show("Selected state code "    event.getValue().getCode());
});

  • Related