adding text to a combobox with a datasource

做~自己de王妃 提交于 2019-12-23 10:09:56

问题


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.


For example:

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:

Persons:
Thomas S.
Lucas B.
Alex X.

Can i achieve that by either manipulating the container or the 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.


回答1:


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.

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);
    }
}



回答2:


If your code is similar to this:

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:


You should do the change in the container (for example: add items...) and call setContainerDataSource(container) again on the combobox (so it gets propagated to the client).



来源:https://stackoverflow.com/questions/14895678/adding-text-to-a-combobox-with-a-datasource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!