2017-05-02 30 views
0

Vaadin 8 Grid中有沒有一種方法可以自動將所有JavaBean-pattern屬性顯示爲表中的列?並自動標記每個列標題的屬性的名稱?在Vaadin 8網格中默認添加我的JavaBean的所有屬性爲列?

綁定到數據部分this page in the Vaadin guide,我們看到這個代碼,我們必須明確指定哪些屬性用作網格中的列。

Grid<Person> grid = new Grid<>(); 
grid.setItems(people); 
grid.addColumn(Person::getName).setCaption("Name"); 
grid.addColumn(Person::getBirthYear).setCaption("Year of birth"); 

回答

4

是的,這是可以通過在bean類型作爲參數傳遞給網格構造:

Grid<Person> grid = new Grid<>(Person.class); 

的JavaDoc:

/** 
* Creates a new grid that uses reflection based on the provided bean type 
* to automatically set up an initial set of columns. All columns will be 
* configured using the same {@link Object#toString()} renderer that is used 
* by {@link #addColumn(ValueProvider)}. 
* 
* @param beanType 
*   the bean type to use, not <code>null</code> 
* @see #Grid() 
* @see #withPropertySet(PropertySet) 
*/ 
1

您可以設置所有列的,使用com.vaadin.data.PropertySet

PropertySet<Person> ps = ...; 
Grid<Person> g = Grid.withPropertySet(ps);` 

對於基於PropertySet反映基於JavaBean的屬性不同,Vaadin提供:

BeanPropertySet.get(Person.class) 

對於標準的用例(其中默認BeanPropertySet是足夠好的),你可以簡單的使用(已經被@JDC回答了):

new Grid<>(Person.class) 
相關問題