Search code examples
vaadinmultiple-columnspojo

Vaadin - Table column order


Anybody know how to/or it is possible - create a Table with column specific order; configuration order which was before save - example in DB, and uploaded at specific view on? also I wonder how to take generate this columns headers and content from POJOS class - beans.

Any good ideas?


Solution

  • setVisibleColumns

    The Table::setVisibleColumns does double-duty:

    • Controls which columns are visible, and
    • Sets the order in which the columns appear.

    Call Table::getVisibleColumns to see current ordering.

    Doc

    This is well described in:

    Example Code

    Basically, you need the code like this to control columns order and also set list of bean instances as datasource.

    Code is not tested, just a demonstration. Valid for Vaadin 6, but I guess no significant changes comparing to Vaadin 7.

    table = new Table();
    
    // Wrap your beans collection into vaadin data container. There are many
    // types of them , check Book of Vaadin.    
    BeanItemContainer<Bean> container = new BeanItemContainer<Bean>(Bean.class)
    container.addBean(new Bean());
    
    // Set collection of your beans as data source. 
    // Columns will be created for each property, vaadin uses reflection.
    table.setContainerDataSource( container );
    
    // You can select to display only properties you want, not all. 
    // Order matters. You can get columns list from any source - for example
    // store in your DB.
    table.setVisibleColumns( new Object[] {"prop1", "prop2"} );
    
    // You can set column headers (by default vaadin will set them same as bean 
    // properties names). Order matters, should match above order.
    table.setColumnHeaders( new String[] {"Property 1", "Property2"} );