Stephane Grenier
Stephane Grenier

Reputation: 15925

How do you get the column order for the Grid in Vaadin 14?

In Vaadin 8 you could just do the following for example to get a list of the columns in the order displayed on the screen.

String columnOrderPreference = ((List<Grid.Column>)grid.getColumns()).stream()
            .map(Grid.Column::getId)
            .collect(Collectors.joining(VALUE_SEPARATOR));

This was especially handy because you could then save that string and then call:

grid.setColumnOrder(columnOrderPreference.split(VALUE_SEPARATOR));

In Vaadin 14 (ignoring that getId() should now use getKey()) this is no longer possible because the getColumns() list is now always returned in the order they were added and not the order in which they are ordered. You can still call setColumnOrder(...) (with different parameters - list of grid.getColumnByKey(columnKey)) but I cannot figure out how to get the list of columns in the order they are displayed.

This is especially useful for trying to save the column order the user has set/changed when they come back to the same page (with the Grid).

Upvotes: 5

Views: 1334

Answers (2)

cfrick
cfrick

Reputation: 37073

You can listen for ColumnReorderEvent on the grid.

Registration addColumnReorderListener(ComponentEventListener<ColumnReorderEvent<T>> listener)

The event holds:

/* Gets the new order of the columns. */
List<Grid.Column<T>> getColumns()

Upvotes: 6

Jean-Christophe Gueriaud
Jean-Christophe Gueriaud

Reputation: 1378

Unfortunately in Vaadin 14 getColumns does not return the columns in the right order. You can get the order when with the event GridReorderEvent as said before and you need to store it. There is a feature request here ( that will give you some technical reasons if you're interested): https://github.com/vaadin/flow-components/issues/1315

You can add a comment or vote for it, because it makes the migration from Vaadin 8 harder.

Upvotes: 3

Related Questions