user1119859
user1119859

Reputation: 719

Vaadin Grid getColumnWidth

I am using Vaadin 24. I use a Grid that contains lots of columns. The columns are resizable. Customer would like to save the widths of the columns. Unfortunately, column.getWidth() always returns null as long as I did not call setWidth() earlier on this column.

So, how can I get the actual width of a column after the user manually resized them?

Adding a listener using addColumnResizeListener will not solve the problem. If the column width changes because the size of the container of the grid changes, the resize listener won't be called although the width of the columns do change.

thanks in advance, Thorsten

Upvotes: 0

Views: 24

Answers (1)

Tatu Lund
Tatu Lund

Reputation: 10643

There is no Java API for that as the widths are not posted to server for performance reasons, but you can fetch the information from browser with async JavaScript call.

grid.getElement().executeJs(
    "const res = []; Array.from(temp1.$.table.rows[0].cells).forEach(col => res.push(col.clientWidth)); return res;")
    .then(res -> { ... do something with res, which is JsonArray ... });

Note, this example is based on assumption you are not having any column grouping, as index 0 is used for rows. If you have column groupings, you may need to use different index.

Upvotes: 1

Related Questions