2017-05-02 29 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) 
相关问题