2015-10-20 57 views
0

我有一個選擇組件(一個組合框是特定的)並添加了一個SQLContainer作爲ContainerDataSource。如何在選擇模式下使用多個ID「屬性」

我已經通過.setItemCaptionPropertyId("myID")設置了ItemCaption。但是,我需要使用兩個屬性作爲標題。

假設ID爲「myID」的屬性表示像「foo」這樣的字符串。還有另一個名爲「myCodeID」的屬性,它表示一個像「23」這樣的數字。

我怎麼能讓我的ComboBox顯示它的項目標題爲「23 foo」?

我在尋找類似.setItemCaptionPropertyIds("myId", "myCodeID")的東西。

+0

看起來你想Concat的這些SQL一邊作爲計算科拉姆 –

+0

不,寧可使用共同的tablequery。但你是對的,我會用這個作爲我的計劃b。 – OddDev

回答

2

我相信有,你可以使用多種方法,但至少以下2種工作方式:

1)快速和

如何利用「假財產」,意思你使用一個物理上並不存在於物體上的物體,那麼這個物體就有一個吸氣體。由於Vaadin will also look for getters/setters確定項目屬性時,它會找到它並將其用於標題。

我知道這不是最優雅的不得不修改你的模型類,但它會讓你在那裏。此外,根據您的實施情況,您也許可以用PersonCaptionGenerator修飾您的Person,其中包含getCaption()方法以使事情保持分離。

假設你有一顆豆,如:

public static class Person { 
     private String name, surname; 
     private int age; 

     public Person(String name, String surname, int age) { 
      this.name = name; 
      this.surname = surname; 
      this.age = age; 
     } 

     public String getName() { 
      return name; 
     } 

     public String getSurname() { 
      return surname; 
     } 

     public void setName(String name) { 
      this.name = name; 
     } 

     public void setSurname(String surname) { 
      this.surname = surname; 
     } 

     public int getAge() { 
      return age; 
     } 

     public void setAge(int age) { 
      this.age = age; 
     } 

     public String getCaption() { 
      // getter to be used as caption 
      return name + " " + surname; 
     } 
    } 

然後,你可以寫這樣的:

public class ComboBoxComponent extends VerticalLayout { 
    public ComboBoxComponent() { 
     BeanItemContainer<Person> dataSource = new BeanItemContainer<>(HasCaption.class); 

     ComboBox comboBox = new ComboBox(); 
     comboBox.setItemCaptionMode(AbstractSelect.ItemCaptionMode.PROPERTY); 

     // use a fake property which will get identified by the getter 
     comboBox.setItemCaptionPropertyId("caption"); 
     addComponent(comboBox); 

     comboBox.setContainerDataSource(dataSource); 
     Random random = new Random(); 
     for (int i = 0; i < 10; i++) { 
      dataSource.addBean(new Person("Name " + i, "Surname " + i, random.nextInt(99) + 1)); 
     } 
    } 
} 

2)延長組合,並添加說明生成

如果您無法修改模型並使用某種通用且可重用的解決方案,則可以擴展ComboBox並覆蓋getItemCaption()方法。請注意,我使用了BeanItemContainer,因爲它更容易,因爲它將bean本身作爲項目ID,但如果需要不同的容器,可能會調整它。

以同樣的Person豆開始,但沒有吸氣劑爲屬性。

通用合同:

public interface CaptionComposer<T> { 
    String getCaption(T item); 
} 

實現我們的Person豆:

private class PersonCaptionGenerator implements CaptionComposer<Person> { 
    @Override 
    public String getCaption(Person person) { 
     return person.getName() + " " + person.getSurname(); 
    } 
} 

自定義組合框會推遲標題檢索到發電機:

public static class MyComboBox<T> extends ComboBox { 
    private CaptionComposer captionComposer; 

    public MyComboBox(CaptionComposer<T> captionGenerator, BeanItemContainer<T> dataSource) { 
     this.captionComposer = captionGenerator; 
     setContainerDataSource(dataSource); 
    } 

    @Override 
    public String getItemCaption(Object itemId) { 
     return captionComposer.getCaption(itemId); 
    } 
} 

最後,添加它到用戶界面:

public class ComboBoxComponent extends VerticalLayout { 
    public ComboBoxComponent() { 
     BeanItemContainer<Person> dataSource = new BeanItemContainer<>(Person.class); 

     addComponent(new MyComboBox<>(new PersonCaptionGenerator(), dataSource)); 

     Random random = new Random(); 
     for (int i = 0; i < 10; i++) { 
      dataSource.addBean(new Person("Name " + i, "Surname " + i, random.nextInt(99) + 1)); 
     } 
    } 
} 

最終都將讓你:

Result