vaadinvaadin-flowvaadin-grid

Vaadin Grid: setAutoWidth(true) with recalculateColumnWidths() works only for visible rows in the grid


After I setup the a Grid with all the columns with setAutoWidth(true) and I execute grid.recalculateColumnWidths() this will auto-adapt the columns width with the content of the columns but only for the rows that are visible in the grid.

What happen is that scrolling rows in the grid I can find some cells with content larger than the content on the first visible rows and they were not auto-adapted and so the content is truncated (es FOO...)

Considering that I am using ListDataProvider, so all the rows are loaded into memory when I build the grid, why it is not calculating the right width for every column considering all the rows?

Is this a normal behaviour? It is possible to change it to consider all the rows when it is calculating the columns width?

I am using Vaadin version 14.

Thanks for the help


Solution

  • Yes,

    this is the normal behaviour of the vaadin grid and is (at least for the 23.x) documented that way

    https://vaadin.com/api/platform/23.2.6/com/vaadin/flow/component/grid/Grid.Column.html#setAutoWidth(boolean)

    The relevant part, why it's not looking at all items in the grid is here:

    For performance reasons the column width is calculated automatically only once when the grid items are rendered for the first time and the calculation only considers the rows which are currently rendered in DOM (a bit more than what is currently visible). If the grid is scrolled, or the cell content changes, the column width might not match the contents anymore.

    You can however "retrigger" a width calculation

    You can manually trigger the auto sizing behavior again by calling Grid.recalculateColumnWidths().