How to do a smart column resize in a WTableView?
Added by Vincenzo Romano 12 months ago
I need to display tabular data and am using the WTableView widget.
I'd like to adapt the column width to the actual content in either of the following "smart" ways:
- The maximum column content width including the header
- The maximum column content width NOT including the header
- The maximum column content width not exceeding a per-column-defined maximum
- Any other smart way I'm not thinking of :-)
Doing the first three would require me to know the actual size of the font to be used. Unless there's something else I'm missing I have no idea on how to get this done this way.
Of course, the first attempt has been to use WTableView::setColumnWidth( <col>,WLength::Auto ) on all the columns.
This produced a table with all columns squeezed to be a few pixel wide, defeating my personal concept of "'auto' length".
Any hint out there?
Replies (8)
RE: How to do a smart column resize in a WTableView? - Added by Koen Deforche 11 months ago
Hey,
This is not supported (partially for the reason you mention: we would need to know server-side how big text turns out client-side).
We could always guess this somewhat server-side, but then so could you buy simply looking at the text length of the model contents per column ?
And yes, 'auto' is not doing that.
Regards,
koen
RE: How to do a smart column resize in a WTableView? - Added by Vincenzo Romano 11 months ago
Just an OT question: what's 'auto' for?
RE: How to do a smart column resize in a WTableView? - Added by Koen Deforche 11 months ago
It's there because in some contexts, WLength::Auto does have a meaning, e.g. as a default width.
It is also explicilty supported in CSS and WLength is modeled after what is available in CSS.
Regards,
koen
RE: How to do a smart column resize in a WTableView? - Added by Vincenzo Romano 11 months ago
I've just tried a first easy approach: calculate the widths in percentage.
It doesn't work and I think it's because the WTableView is not a <TABLE>.
How can I guess the width of the font being used in a WTableView?
RE: How to do a smart column resize in a WTableView? - Added by Vincenzo Romano 11 months ago
Any hint?
RE: How to do a smart column resize in a WTableView? - Added by Koen Deforche 11 months ago
Hey,
There is no accurate way of knowing the font size.
This width you can get in a layoutSizeChanged() call (after doing setLayoutSizeAware()), in a specialized WTableView class.
With Wt 3.2.1 this call will be invoked when your table is in a layout only, but in Wt 3.2.2 this event will be delivered even if the table is not in a layout (because it uses a layout itself).
Regards,
koen
RE: How to do a smart column resize in a WTableView? - Added by Vincenzo Romano 11 months ago
I'd like to go the percentage way, Koen, as it should be better when supporting different resolution screen.
Is there a way to express the column width in % of the container width?
RE: How to do a smart column resize in a WTableView? - Added by Koen Deforche 11 months ago
Hey Vincenzo,
Using setLayoutSizeAware() + layoutSizeChanged() you can react to the change of the table width.
Then you can work out an appropriate width for each column, taking into account 7 pixels that are used for padding and border (thus you need to substract (7 * model->columnCount()) from the total width and divide that width to the columns.
Regards,
koen
(1-8/8)