How can I make a table column shrink to fit the longest width? #517
Replies: 3 comments 13 replies
-
This is not possible. Doing so would require measuring the width of the text in all rows of the table before rendering in order to calculate the widths of all of the columns. The column widths are pre-computed based on the available width of the table's container. Then based on those now-fixed column widths, it determines the height needed per row. It might be possible to modify the code to allow one or two "auto" columns, but you'd risk running into a layout exception if you had unexpectedly long text. This would force any other columns to be too small to hold content. |
Beta Was this translation helpful? Give feedback.
-
Here's a branch that I created that hacks an auto-column option in to the column definition. During the draw stage, QuestPDF updates the column widths, but the Cells enumerable is empty, so it has to skip over the text measurement during that phase. I don't know that this is the best way to handle this, but it does work in my testing. https://github.com/flew2bits/QuestPDF/tree/table-auto-column |
Beta Was this translation helpful? Give feedback.
-
@MarcinZiabek In my project, I would also need columns that automatically adjust the width. My suggestion would be to add 2 new properties to the columns. AllowShrink and AllowGrow. If the table is created, you could first calculate the widths as they are currently calculated. Then you would go through all the columns that have AllowShrink set to true and shrink the columns (if possible). Then you would widen all columns (if any) that have AllowGrow set to true until the width of the columns matches the width of the table. Could I create a PR in which I implement this for the ConstantColumn as well as for the RelativeColumn? And would you then also look at the PR? |
Beta Was this translation helpful? Give feedback.
-
If I have a table with multiple rows, how can I make a column (say, the first column) shrink to fit the longest text in any of the cells without wrapping the text or specifying a constant width for the column?
Beta Was this translation helpful? Give feedback.
All reactions