使用数据源向组合框添加文本

5
我有一个Vaadin的下拉框,它使用一个ContainerDataSource来填充数据。
setContainerDataSource(container);

我现在想在结果列表中的某个位置插入一个静态文本。
例如:
一个下拉框,其中填充了一个容器,而在结果列表中弹出的第一个条目是某种标题:
人员: Thomas S. Lucas B. Alex X.
我能否通过操纵容器或下拉框来实现这一点?
我尝试设置容器源并通过addItem()向ComboBox添加String / Label,但似乎不起作用。我有点新手,所以不知道该如何继续。

如果您发布一下您已经尝试过或者研究过的内容,可能会有所帮助。 - Aaron Kurtzhals
我刚试图设置容器源并通过addItem()向ComboBox添加一个String/Label,但似乎不起作用。我有点新手,所以不知道该如何继续。 - luuksen
3个回答

6
如果您正在将ComboBox用作立即使用,并且不希望"Person:"被视为真实人员,您可以使用setNullSelectionItemId 将虚假的人物定义为真正的虚拟对象。但是这种解决方案的限制是只能添加一个虚拟对象。
以下是我的示例,它在列表顶部添加了"Person:"并将其视为null值。请注意,我正在使用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作为结果获取。 - Bourkadi

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

0

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


我也尝试过,我有一个填充了人的Container<Person>,并尝试container.addItem(new String("test"));“test”不会显示在列表中,但容器的其余部分会显示。 - luuksen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接