table: Size property not working
Describe the bug
I am using v8 and unable to change the width of the column of the most simple example according to the document.
https://tanstack.com/table/v8/docs/examples/react/basic
For this example, I opened the sandbox and changed the size of the first column to 500, as below:
columnHelper.accessor('firstName', {
cell: info => info.getValue(),
footer: info => info.column.id,
size: 500,
minSize: 500
}),
However the width of the first column remain unchanged
Your minimal, reproducible example
https://codesandbox.io/p/sandbox/strange-pine-wtqhtg?embed=1&file=%2Fsrc%2Fmain.tsx%3A62%2C15
Steps to reproduce
Add size property to the first column of basic example one
Expected behavior
Size property should be respected
How often does this bug happen?
Every time
Screenshots or Videos
No response
Platform
Chrome
react-table version
8.10.3
TypeScript version
No response
Additional context
No response
Terms & Code of Conduct
- I agree to follow this project’s Code of Conduct
- I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 23
- Comments: 26 (7 by maintainers)
@anduscheung , @aoloo
You’re probably missing the part where you set the width by yourself in the styles prop:
It’s shown here: Tanstack Column Sizing
I’m using tanstack table in shadcn/ui. Columns sizing didn’t work until i add 1.
Thanks, I thought it is an automatic property 👍
I am using shadcn with Tanstack table and solved my issue by adding a defaultColumn prop to give columns a size.
I also needed to change the CSS in TableHead to use width : header.getSize()
Hope it helps someone if they are using shadcn with TanStack table. 😃
This was the missing piece! Somehow the table was overriding the cell widths but this fixed it
I’m using shadcn-ui v0.8.0 and in the TableHeader components I used header.column.columnDef.meta?.width
Then in my column definition i used the metadata object with the width value. Not sure if this is proper but it worked.
Hopefully this helps.
I imagine this same approach would make the size, minSize, and maxSize work as well.
Worked for Chakra UI too.
Thanks, but what if in other scenario I need to set a 150px size to a column, then it would be ignored.
I believe there should be a different attribute like
defaultWidthor something, then I could decide if use it or not. For now I ended up doing something similar to your response but defining default width to a size equals to de minSize +1That way, if someone sets a size of 51 it will be ignored but the default being 50 then there should be not much of a difference. I know it is a poor implementation but can’t find another way
@Raphaww, how about this implementation?
width: header.getSize() !== 150 ? header.getSize() : undefinedWhat if I just want to add width to one column?
the
getSizefunction returns the default (150) in every other columnand I want them to remain unset
The issues that most seem to run into here is that many people assume that TanStack Table will hook up the width CSS for you. It doesn’t! You need to set the styles on your
<table>,<th>, and/or<th>elements. The ShadCn components don’t do this out of the box.every answer here is working with size, but not with maxSize. Have someone any workaround with this?
Just don’t apply a width at all in your styles if you want auto fit (assuming semantic table elements). The size property is for specifying an exact size.