9

ComboBoxユーザーが連絡先リストから連絡先を選択できるようにするを定義しました。ComboBox は連絡先名を表示していますが、実際の連絡先へのマッピングには使用できません。連絡先 ID が必要です。私の問題は、リンクされた値と ID を入力する方法がわからずVaadin ComboBox、値のみを表示することです。

// Add all organization contacts to the drop-down
for (Contact contact : organizationContacts) {
    contactName = contact.getName();
    contactId   = contact.getId();
    _logger.debug("Adding contactName=" + contactName + " contactId=" + contactId + " to person with id=" + personId);
    contactNameCombo.addItem(contactName);
}

// Add the contact of this person, and select it in the drop-down
contactName = person.getContact().getName();
contactId   = person.getContact().getId();
contactNameCombo.addItem(contactName);
contactNameCombo.setValue(contactName);

上記のコードでわかるように、 を に追加しcontactNameていますが、データベースを更新するために使用する必要がある ID を選択したエントリから後で知ることができるように、ComboBoxも追加する方法がわかりません。contactId

4

4 に答える 4

11

これにアプローチするにはいくつかの方法があります。ここで最も柔軟なのは、名前付きプロパティをキャプションとして使用するようにコンボボックスを構成することです。詳細について は、アイテム の選択に関するブック オブ ヴァーディンを参照してください。

// Set the caption mode to read the caption directly
// from the 'name' property of the item
contactNameCombo.setItemCaptionMode(Select.ITEM_CAPTION_MODE_PROPERTY);
contactNameCombo.setItemCaptionPropertyId("name");

// Add all organization contacts to the drop-down
for (Contact contact : organizationContacts) {
    contactName = contact.getName();
    contactId   = contact.getId();
    _logger.debug("Adding contactName=" + contactName + " contactId=" + contactId + " to person with id=" + personId);

    // Note : the itemId of the item is the contactId
    Item item = contactNameCombo.addItem(contactId);
    item.getProperty("name").setValue(contactName)
}
// Add the contact of this person, and select it in the drop-down
contactName = person.getContact().getName();
contactId   = person.getContact().getId();
Item item = contactNameCombo.addItem(contactId);
item.getProperty("name").setValue(contactName)

// Using the itemId (which = contactId) to select the given contact
contactNameCombo.setValue(contactId);
于 2012-05-30T08:15:06.463 に答える
10

@Charles Anthony が提供した解決策もうまくいきませんでした。Vaadin ドキュメント ( https://vaadin.com/book/-/page/components.selecting.html ) で、次のコードを見つけました。

// Set item caption for this item explicitly
select.addItem(2); // same as "new Integer(2)"
select.setItemCaption(2, "Deimos");

これは私のために働きます。

于 2013-02-25T12:55:53.080 に答える
4

ヴァーディン7:

    statusSelectCombo.setItemCaptionMode(ItemCaptionMode.PROPERTY);
statusSelectCombo.setItemCaptionPropertyId("courseOptionValue");

   IndexedContainer iContainer = new IndexedContainer();
   iContainer.addContainerProperty("courseId", String.class, "");
   iContainer.addContainerProperty("courseOptionValue", String.class, "");
    String addItemId="";
    String addItemCaption="";
for (int i = 0; i < comboItemsArray.length; i++) //String[] comboItemsArray
{
    log.debug("comboItemsArray["+i+"] "+comboItemsArray[i]);
    addItemId= comboItemsArray[i];
    addItemCaption=comboItemsArray[i];
   Item newItem = iContainer.getItem(iContainer.addItem());
   newItem.getItemProperty("courseId").setValue(addItemId);
   newItem.getItemProperty("courseOptionValue").setValue(addItemId);
}
statusSelectCombo.setContainerDataSource(iContainer);

ValueChangeListener listener = new Property.ValueChangeListener()
{
    public void valueChange(ValueChangeEvent event)
    {
    statusSelectCombo.getItemIds();
    Property changedProperty = event.getProperty();
    Object selectedStatus = (Object) statusSelectCombo.getValue(); //it is get Value but gives object ID as an Object
    Item rowItem = statusSelectCombo.getItem(selectedStatus);
    final String selectedCourseId = (String) rowItem.getItemProperty("courseId").getValue();        

    }
};
于 2014-06-17T11:52:24.963 に答える
2

チャールズ・アンソニーは完全に正しいです。

たとえば、BeanContainer や BeanItemContainer などのコンテナ (詳細はこちら) を利用して、連絡先オブジェクトを ComboBox に追加することもできます。コンテナをいっぱいにして追加する必要があります

contactNameCombo.setContainerDataSource(YOUR_CONTAINER);

あなたのコンボボックスに。

于 2012-05-30T10:15:20.753 に答える