我有一个 vaadin 组合框,里面装有容器数据源
setContainerDataSource(container);
我现在想在结果列表中的某处插入静态文本。
例如:
一个组合框充满了一个容器,结果列表中弹出的第一个条目是某种标题:
Persons:
托马斯·S。
卢卡斯·B.
Alex X.
我可以通过操作容器或组合框来实现这一点吗?
我只是尝试设置容器源并通过 addItem() 添加字符串/标签到组合框,但这似乎不起作用。我对此有点陌生,所以我不知道如何继续。
如果您将 ComboBox 作为直接使用,并且不希望将“Person:”作为真人处理,则可以使用setNullSelectionItemId https://vaadin.com/api/com/vaadin/ui/AbstractSelect.html#setNullSelectionItemId%28java.lang.Object%29将假人定义为真正的虚拟对象。然而,这一解决方案有一个限制,即您只能添加一个虚拟对象。
这是我的示例,它将“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);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)