首页 > 解决方案 > Vaadin - 如何从 bean 项目容器中 bean 项目的对象 id 获取 bean 本身

问题描述

我为 ComboBox 设置了一个 ContainerDataSource this.comboBox.setContainerDataSource(container)。这个容器是一个 BeanItemContainer。此外,我创建TextFields了绑定到 .bean 的某些属性的BeanItemContainer. 为了获取ComboBoxI only can do的选定数据this.comboBox.getValue(),它返回选定 bean 的对象 id。我如何使用这些信息来获取实际选择的 bean 本身?我需要它来设置字段组中我的文本字段的项目数据源。

final BeanItemContainer<Person> personContainer = new BeanItemContainer<>(Person.class);
for(int h = 0; h <= this.table.getSelectedItems().size() -1; h++) {
final Person person = this.table.getSelectedItems().get(h).getBean();    
personContainer.addBean(person);}
final Window win = new Window("Person", new PersonView(personContainer));
this.getUI().addWindow(win);

人物视图:

public PersonView(final BeanItemContainer<Person> personContainer) {
    super();
    this.initUI();

    this.comboBox.setContainerDataSource(personContainer);
    this.comboBox.setItemCaptionMode(ItemCaptionMode.PROPERTY);
    this.comboBox.setItemCaptionPropertyId("name");

    }
private void comboBox_valueChange(final Property.ValueChangeEvent event) {
    //this.fieldGroup.setItemDataSource(...);
}

标签: javavaadin

解决方案


我假设您使用的是 Vaadin 7。

这是从 ComboBox 获取所选项目的解决方案:

        comboBox.addValueChangeListener( new ValueChangeListener() {

            @Override
            public void valueChange(ValueChangeEvent event) {

                Person person = (Person) comboBox.getValue();

                //do something with the selected value
                this.fieldGroup.setItemDataSource(person);              
            }
        });

推荐阅读