将文本添加到具有数据源的组合框

将文本添加到具有数据源的组合框

本文介绍了将文本添加到具有数据源的组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个vaadin组合框,其中充满了containerdatasource

I have a vaadin combobox that is filled with a containerdatasource

setContainerDataSource(container);

我现在想在结果列表中的某处插入静态文本.

I now want to insert a static text somewhere in the list of results.

例如:

一个填充有的容器的组合框,并且在结果列表中弹出的第一个条目是某种标题:

A Combobox that is filled with a container of and the first entry that pops up in the result list is some kind of header:

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

Persons:
Thomas S.
Lucas B.
Alex X.

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

Can i achieve that by either manipulating the container or the combobox?

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

I just tried to set the container source and add a String/Label via addItem() to the ComboBox, but that doesn't seem to work. I am kinda new to this, so I don't know how to continue.

推荐答案

如果您立即使用ComboBox,并且不希望将"Person:"作为真实的人来处理,则可以使用将假人定义为一个真正的虚拟对象.但是,此解决方案的局限性是只能添加一个虚拟对象.

If you are using the ComboBox as immediate and don't want the "Person:" to be handled as a real person, you could use setNullSelectionItemId to define the fake person as a true dummy object. This solution, however, has the limitation that you can only add one dummy object.

这是我的示例,该示例在列表顶部添加人:"并将其作为空值进行处理.请注意,我正在使用Vaadin 7.

Here's my example which adds "Person:" on top of the list and handles it as a null value. Note that I'm using 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);
    }
}

这篇关于将文本添加到具有数据源的组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 20:45