5

containerdatasourceで満たされたvaadinコンボボックスがあります

setContainerDataSource(container);

結果のリストのどこかに静的テキストを挿入したいと思います。


例えば:

のコンテナで満たされたコンボボックスと、結果リストにポップアップ表示される最初のエントリは、ある種のヘッダーです。

人物:
トーマスS.
ルーカスB.
アレックスX。

コンテナまたはコンボボックスを操作することでそれを達成できますか?

コンテナソースを設定し、addItem()を介してComboBoxに文字列/ラベルを追加しようとしましたが、うまくいかないようです。私はこれに少し慣れていないので、続行する方法がわかりません。

4

3 に答える 3

6

ComboBox を即時として使用していて、「Person:」を実在の人物として処理したくない場合はsetNullSelectionItemId、偽の人物を真のダミー オブジェクトとして定義するために使用できます。ただし、このソリューションには、ダミー オブジェクトを 1 つしか追加できないという制限があります。

リストの一番上に "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);
    }
}
于 2013-02-20T19:32:36.183 に答える
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
于 2013-02-16T11:35:25.320 に答える
0

コンテナで変更を行い(例:アイテムを追加...)、コンボボックスでsetContainerDataSource(container)を再度呼び出す必要があります(クライアントに伝達されます)。

于 2013-02-15T14:05:10.117 に答える