Help with CSS for resizing columns
edit: repro: https://stackblitz.com/edit/tanstack-table-7djbwc?file=src/main.tsx
Anyone mind taking a look
With this resize in SS #2 you can see the columns sizes are off. The internals are
{ actions: 1165, firstName: 0, nickName: 817 }
but you can see firstName is not the 60px width.
Also when i add style={{ width: table.getTotalSize() }}
my table doesn't take up w-full like it did before (i believe all the cols are at 120px while the "default" size is 150.
When I don't use getTotalSize(), the columns don't adjust (but the code does)
StackBlitz
Shadcn DataTable Column Resizing Performant Example (forked) - Stac...
Run official live example code for Table Column Resizing Performant, created by Tanstack on StackBlitz


9 Replies
rival-blackOP•2y ago
@KevinVandy I added a repro https://stackblitz.com/edit/tanstack-table-7djbwc?file=src/main.tsx
based on your example https://tanstack.com/table/latest/docs/framework/react/examples/column-resizing-performant
Would you mind taking a quick look? You'll notice when you resize a col, it resizes the other columns (but the values in
columnSizing
don't adjust.
I think its maybe a way im applying CSS but not totally surenational-gold•2y ago
why don't the
<th>
elements have a width style?
You just had a bunch of problems with your table head and table styles
Don't delete the * 1px. That's importantrival-blackOP•2y ago
it looks like i had a typo and forgot to pass in
1px
, thats one of those problems lolnational-gold•2y ago
got it working
rival-blackOP•2y ago
mind sharing what you did?
national-gold•2y ago
Simply fixed those styles with the proper* 1px calc and correct ending parenthesis
Also correct colSpan on the headers
rival-blackOP•2y ago
🙌 thanks! that was what i was missing
colSpan={header.colSpan}
in TableHeadrival-blackOP•2y ago
With
<Table className='overflow-auto' style={{ ...columnSizeVars, width: table.getTotalSize() }}>
getTotalSize is setting it to the initial size of all the columns as the default of 150 or 200 as I understand. Do you think the best approach to evenly distribute teh size of the columsn within a parent to use a ref and useeffect to set it on load?
rival-blackOP•2y ago
@KevinVandy actually what prob makes more sense for my usecase is distrbuting only the non-actions/goTo columns across the parent element. I'm trying to do something like this but
columnDef
= 970px while my logic has the parent width of 1022px.
Tried this by adding a loading state as well but totalColumnSize
is the array that adds up to the 970px instead of the 1022px.
Curious to hear if you had a better recommendation