Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 967
PDS Avatar asked Oct 22 '13 21:10

PDS


2 Answers

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:

  • Book of Vaadin > Table
  • Sampler > User Interface > Data Presentation > Table
  • Table API JavaDoc

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"} );
like image 102
Sergey Makarov Avatar answered Sep 24 '22 15:09

Sergey Makarov


The answer by Sergey Makarov is correct. This answer provides further information.

User’s Reordering

You may allow the user to drag-and-drop columns in a table to re-order them at runtime. To enable this feature, call isColumnReorderingAllowed.

You can use a listener to be informed when such a reorder event occurs.

The user’s re-ordering lasts only for this work session. If you want to maintain the user’s order in future work sessions, you must somehow persist their desired order and apply the order when instantiating the table again.

Losing The Order

If you replace the data source of the table, your column order will be reset. You can get the current order before the change, then restore. Example code follows.

// Visible status & ordering.
java.lang.Object[] visibleColumns = this.exampleTable.getVisibleColumns();

// ------|  Fresh Data  |--------------
// Put fresh data into the Table.
this.exampleTable.setContainerDataSource( freshBeanItemContainer );

// ------|  Restore Config  |--------------
// Visible status & ordering.
this.exampleTable.setVisibleColumns( visibleColumns ); // Assumes the table has the same properties.

By the way, as with visibility and order, being collapsed will also be reset with fresh data. You may want to track and restore column collapsing as well as ordering.

like image 22
Basil Bourque Avatar answered Sep 24 '22 15:09

Basil Bourque