【问题标题】:adding text to a combobox with a datasource使用数据源将文本添加到组合框
【发布时间】:2023-12-18 12:33:01
【问题描述】:

我有一个填充了容器数据源的 vaadin 组合框

setContainerDataSource(container);

我现在想在结果列表的某处插入一个静态文本。


例如:

一个 Combobox 填充了一个容器,结果列表中弹出的第一个条目是某种标题:

人员:
托马斯·S.
卢卡斯 B.
亚历克斯 X。

我可以通过操作容器或组合框来实现吗?

我只是尝试设置容器源并通过 addItem() 将字符串/标签添加到 ComboBox,但这似乎不起作用。我对此有点陌生,所以我不知道如何继续。

【问题讨论】:

  • 如果您发布到目前为止您已经尝试或研究过的内容可能会有所帮助。
  • 我只是尝试设置容器源并通过 addItem() 将字符串/标签添加到 ComboBox,但这似乎不起作用。我对此有点陌生,所以我不知道如何继续。

标签: java combobox vaadin


【解决方案1】:

如果您将 ComboBox 用作直接对象并且不希望将“Person:”作为真人处理,则可以使用 setNullSelectionItemId 将假人定义为真正的虚拟对象。但是,此解决方案有一个限制,您只能添加一个虚拟对象。

这是我在列表顶部添加“Person:”并将其作为空值处理的示例。请注意,我使用的是 Vaadin 7。

import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.AbstractSelect;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Notification;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

/**
 * The Application's "main" class
 */
@SuppressWarnings("serial")
public class MyVaadinUI extends UI {

    @Override
    protected void init(VaadinRequest request) {
        final VerticalLayout layout = new VerticalLayout();
        layout.setMargin(true);
        setContent(layout);

        BeanItemContainer<Person> container = new BeanItemContainer<Person>(Person.class);
        Person nullPerson = new Person(0, "Person:");
        container.addBean(nullPerson);
        container.addBean(new Person(1, "Django"));
        container.addBean(new Person(2, "Schultz"));

        ComboBox combobox = new ComboBox();
        combobox.setImmediate(true);
        combobox.setNullSelectionItemId(nullPerson); // Define the null person as a dummy.
        combobox.setContainerDataSource(container);
        combobox.setItemCaptionMode(AbstractSelect.ItemCaptionMode.PROPERTY);
        combobox.setItemCaptionPropertyId("name");  // the person's name field will be shown on the UI
        combobox.addValueChangeListener(new Property.ValueChangeListener() {
            @Override
            public void valueChange(ValueChangeEvent event) {
                // Will display 'null selected' when nullPerson is selected.
                Notification.show(event.getProperty().getValue() + " selected");
            }
        });

        layout.addComponent(combobox);
    }
}

【讨论】:

  • 很好,但我想知道如何获取此人的 ID
【解决方案2】:

如果你的代码类似这样:

BeanItemContainer<Person> container = new BeanItemContainer<Person>(Person.class);
container.addAll(myPersonList);
ComboBox combobox = new ComboBox();
combobox.setContainerDataSource(container);
combobox.setItemCaptionMode(Select.ITEM_CAPTION_MODE_PROPERTY);
combobox.setItemCaptionPropertyId("name");  // the person's name field will be shown on the UI

// imho if you want to add a static text (String) into a container
// which populated with Person objects then you have to make a fake Person object
Person staticText = new Person();
staticText.setName("My static text");
combobox.addItem(staticText);
// if you want to specify the index of the item, add them one by one in for cycle
// and insert the static text item in the appropritate place

【讨论】:

    【解决方案3】:

    您应该在容器中进行更改(例如:添加项目...)并在组合框上再次调用 setContainerDataSource(container)(以便将其传播到客户端)。

    【讨论】:

    • 也试过了,我有一个装满人的 Container 并尝试了 container.addItem(new Sring("test")); “test”不会显示在列表中,但容器的其余部分会显示。
    最近更新 更多