2013-12-20 29 views
0

有沒有一種方法來實現與PropertyId相關的特定字段的TableFactory接口? 我只得到一個類型的字段,因爲我使用的是通用類爲我所有的表,和我丟失的CheckBox布爾值(Groovy代碼):Vaadin 7:TableFieldFactory

class DefaultTableFieldFactory implements TableFieldFactory { 
    @Override 
    public Field<?> createField(Container container, Object itemId, Object propertyId, Component component) { 
     TextField t = new TextField() 

     switch(propertyId) { 
      case "firstname": t.setNullRepresentation(""); 
      case "lastname": t.setNullRepresentation(""); 
      case "mobile": t.setNullRepresentation(""); 
      case "tel": t.setNullRepresentation(""); 
      case "email": t.setNullRepresentation(""); 
      default: break; 
     } 
     t.setWidth("95px") 

     return t 
    } 
} 

,所以我需要使用上面這個類其中包含DefaultTableFieldfactory,以便在整個應用程序中將null表達式設置爲「」(而不是「null」)。

我們的目標是在一個地方爲我的自定義組件(超過30個)提供這種空表示,我想將這個類用作每個表的默認工廠,並像以前那樣連接它:

def contacts = (Grails.get(FundService)).getAllContacts(fundId) 
     def cContainer = new BeanItemContainer<Contact>(Contact.class,contacts) 


     def t = new Table() 
     t.containerDataSource = cContainer 
     t.setTableFieldFactory(new DefaultTableFieldFactory()) 

回答

1

Vaadin提供DefaultTableFieldFactory其確實地圖

  • 日期到一個DateField
  • 布爾將CheckBox
  • OTH呃到TextField

DefaultTableFieldFactory已經在表上設置。所以在你的情況下,如果你只想爲你的布爾字段使用CheckBox,我不會實現自己的TableFieldFactory。這裏有一個例子:

Table table = new Table(); 

table.addContainerProperty("text", String.class, ""); 
table.addContainerProperty("boolean", Boolean.class, false); 
table.setEditable(true); 

Object itemId = table.addItem(); 
table.getItem(itemId).getItemProperty("text").setValue("has accepted"); 
table.getItem(itemId).getItemProperty("boolean").setValue(true); 

如果你真的需要有自己的TableFieldFactory然後Vaadin建議:

你可以只實現TableFieldFactory接口,但我們 建議您根據延長DefaultFieldFactory您需要 。在默認實現中,映射在 createFieldByPropertyType()方法中定義(您可能想要查看 源代碼),無論是表格還是表格。

在您提供的問題代碼中,您總是返回一個TextField。對於你丟失的複選框,你需要在特定情況下返回一個複選框。

使用FieldFactories時不要忘記setEditable(true)

更多信息here根據5.16.3。編輯表中的值。

+0

謝謝,但我已經知道什麼是DefaultTableFactory,因爲我在我的代碼中實現它。此處的目標是提供此工廠以在單個工廠中處理不同類型的字段,以便將此模式應用於表的容器數據源(請參閱上面修改的代碼) –

+1

@ludo_rj然後查看createFieldByType()方法Vaadin提供的DefaultFieldFactory類在適當時返回CheckBox。 – nexus

+0

是的,這就是我正在尋找的:)因爲DefaultFieldFactory.createFieldByPropertyType是靜態的,所以它不能被覆蓋,所以它需要額外的代碼才能在TableFieldFactory中正確實現。 –