Grid Pro具有三个推荐的内置编辑器:文本字段、复选框和选择。您如何管理具有关键字和值属性的列表?例如:国家列表
我无法在选择值中显示国家名称,也无法在数据库记录中保存id。当我管理一个包含键和值的列表时,我通常依赖于ComboBox。组合框不在内置编辑器的列表中。你建议自己写编辑吗?
假设您有一个属性为id
和name
的Country
类。以及一个具有country
性质的Person
类。您将显示GridPro<Person>
。
您可以制作一个Select
编辑器,并定义它的itemLabelGenerator(或者渲染器,如果您想显示标志或其他东西(来显示Country::getName
。**
保存到数据库应该由关系上的注释来处理。通常使用JPA,Person和Country之间会有@ManyToOne关系,并将国家FK(id,使用@id指定(保存在Person表中。
**我不使用GridPro,但在检查代码时,我可以看到使用gridPro.addEditColumn(Person::getCountry).select(Person::setCountry, countriesList)
无法指定itemLabelGenerator/renderer
但是,您可以准备自己的Select
组件并使用EditColumnConfigurator::custom
方法。
Select<Country> countryEditorComponent = new Select<>();
countryEditorComponent.setItems(countriesList);
countryEditorComponent.setItemLabelGenerator(country -> country.getName());
gridPro.addEditColumn(Person::getCountry).custom(countryEditorComponent , Person::setCountry).setHeader("Country");