Vaadin - Table column order

Does anyone know how / or this is possible - create a table with column order; the configuration order that was before saving - an example in the database, and loaded into a specific view? It's also interesting how to generate the headers and contents of columns from the POJOS class - beans.

Any good ideas?

+6
source share
2 answers

setVisibleColumns

Table::setVisibleColumns performs a dual mode:

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

Call Table::getVisibleColumns to see the current order.

Doc

This is well described in:

Code example

Basically, you need such code to control the order of the columns, as well as a list of bean instances as the data source.

The code is not tested , just a demo. Valid for Vaadin 6, but I don’t think that there are any significant changes compared 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"} ); 
+8
source

The correct answer is Sergey Makarov . This answer contains additional information.

User reordering

You can allow the user to drag and drop columns in the table to reorder them at run time. To enable this function, call isColumnReorderingAllowed .

You can use the listener to get information about the occurrence of such a reordering event.

User reordering lasts only for this work session. If you want to maintain the order of users in future work sessions, you must somehow maintain your desired order and apply the order when creating the table again.

Loss of order

If you replace the table data source, the column order will be reset. You can get the current order before the change, and then restore. The following is sample code.

 // 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, when minimized, it will also reset with fresh data. You can track and restore collapse of a column, as well as order.

+2
source

Source: https://habr.com/ru/post/956509/


All Articles