'Vaadin grid - gridpro ComboBox Editor

Grid Pro features three recommended built-in editors: Text Field, Checkbox, and Select. Ho can you manage a list with key and value properties? For example: Country list

I'm unable to show country name in the select value and save the id in the database record. I usually rely on ComboBox when I manage a list with key and value. Combobox is not present in the list of built-in editors. Do you suggest to write your own editor?



Solution 1:[1]

Let's assume you have a Country class with the properties id and name. And a Person class with a country property. You display a GridPro<Person>.

You can make a Select editor and define it's itemLabelGenerator (or renderer, if you want to show a flag or something) to show Country::getName. **

The saving to the database should be handled by the annotations on the relation. Usually using JPA, there would be a @ManyToOne relation between Person and Country, and save the country-FK (id, specified using @Id) in the person table.

** I don't use Grid Pro, but upon inspection of the code I can see that by using gridPro.addEditColumn(Person::getCountry).select(Person::setCountry, countriesList) you cannot specify the itemLabelGenerator/renderer.
However, you could prepare your own Select component and use the EditColumnConfigurator::custommethod.

Select<Country> countryEditorComponent = new Select<>();
countryEditorComponent.setItems(countriesList);
countryEditorComponent.setItemLabelGenerator(country -> country.getName());
gridPro.addEditColumn(Person::getCountry).custom(countryEditorComponent , Person::setCountry).setHeader("Country");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1