2016-08-03 8 views
2

としてメンバオブジェクトのフィールドを設定し、私は以下のシナリオを持っていると仮定しますVaadinグリッドSetColumn関数() - 列

2つのエンティティがあります。

public class Address { 

    private City city; 
    private String street; 

    public String getStreet() { 
    return street; 
    } 

    public void setStreet(String street) { 
    this.street = street; 
    } 

    public City getCity() { 
    return city; 
    } 

    public void setCity(City city) { 
    this.city = city; 
    } 
} 

そして

public class City { 

    private String name; 

    public String getName() { 
    return name; 
    } 

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

は私がしたいですBeanItemContainerを使用してグリッドコンポーネントにストリートとシティの名前を表示するには、どのように列名を指定する必要がありますか?

Pは、「ストリート」及び「city.name」を使用しようとしたが、それは例外をスローします。

java.lang.IllegalStateException: Found at least one column in Grid that does not exist in the given container: city.name with the header "Name". Call removeAllColumns() before setContainerDataSource() if you want to reconfigure the columns based on the new container. 

答えて

3

あなたは(便宜上、私はあなたのオブジェクトのためのいくつかのコンストラクタを作成したことに注意してください)しかし、あなたは以下のことをやって、少なくとも2つの方法を見ることができ、グリッドに関連するコードを示さなかった。

コード:

public class MyUi extends UI { 
    @Override 
    protected void init(VaadinRequest request) { 
     // basic stuff 
     Layout content = new VerticalLayout(); 
     content.setSizeFull(); 
     setContent(content); 

     // container & grid 
     BeanItemContainer<Address> container = new BeanItemContainer<>(Address.class); 
     Grid grid = new Grid(container); 

     // 1) either manually add nested properties and hide the actual inner bean 
     container.addNestedContainerProperty("city.name"); 
     grid.getColumn("city.name").setHeaderCaption("City"); 
     grid.setColumns("street", "city.name"); // hide bean column 

     // 2) or make the container create nested properties for your inner beans 
     container.addNestedContainerBean("city"); 
     grid.getColumn("city.name").setHeaderCaption("City"); 

     // create some dummy data to populate the grid 
     City city = new City("There"); 
     Address address = new Address(city, "Here"); 
     container.addItem(address); 
     content.addComponent(grid); 
    } 
} 

結果:

Sample

関連する問題