-
I would like to be able to mix fixed and auto-sized (depending of their content) width columns, like this : const columns = React.useMemo(
() => [
{
Header: "First Name",
accessor: "firstname",
width: 300, //Fixed width
},
{
Header: "Last Name",
accessor: "lastname",
//No width defined, would like it to be auto
},
{
Header: "Age",
accessor: "age",
width: 300, //Fixed width
},
{
Header: "Option",
accessor: "select",
//No width defined, would like it to be auto
},
],
) The problem is that for the columns where i didn't set the width value, the default value is 150 (I would like to set it as auto). So in my table rendering, I have to look for width !== 150 before setting width, which seems weird. Thanks for your help! |
Beta Was this translation helpful? Give feedback.
Replies: 16 comments 29 replies
-
The solution I found is to change the default column width property to auto, and override the default width of 150. const defaultColumn = {
width: "auto",
} See: |
Beta Was this translation helpful? Give feedback.
-
For anyone still looking for an alternative, this is how I implemented it in React const table = useReactTable({
data: data,
columns: columns,
getCoreRowModel: getCoreRowModel(),
defaultColumn: {
minSize: 0,
size: Number.MAX_SAFE_INTEGER,
maxSize: Number.MAX_SAFE_INTEGER,
}
}) Header width calculation: style={{width: header.getSize() === Number.MAX_SAFE_INTEGER ? "auto" : header.getSize()}} Cell width calculation: style={{width: cell.column.getSize() === Number.MAX_SAFE_INTEGER ? "auto" : cell.column.getSize()}} |
Beta Was this translation helpful? Give feedback.
-
Any non-hacks to set custom initial width of |
Beta Was this translation helpful? Give feedback.
-
For people looking for a fast (and maybe not that pretty solution)
This ColumnDef example works like a charm. |
Beta Was this translation helpful? Give feedback.
-
the first column doesn't apply the correct width |
Beta Was this translation helpful? Give feedback.
-
Yeah, the calculations seem off. Content is size of |
Beta Was this translation helpful? Give feedback.
-
If I may, this is quite an annoying gap in an otherwise good table library. I want the table to default to the screen size when it is first rendered, which I think is quite natural. I ended up solving this in a very roundabout way which still has a bunch of weirdness associated with it:
|
Beta Was this translation helpful? Give feedback.
-
I think that people don't realize that getSize() is part of the Column Sizing feature, and it's not ment to retrieve string values. You'll need to add a property to the ColumnMeta that you want to use on your import "@tanstack/react-table";
declare module "@tanstack/table-core" {
interface ColumnMeta<TData extends RowData, TValue> {
className?: string;
style?: CSSProperties;
width?: Property.Width<string | number>
}
} |
Beta Was this translation helpful? Give feedback.
-
maybe there can try this way: <colgroup>
{columns.map((col, index) => (
<col key={col.header as string} style={{ width: col.size || 200 }} />
))}
</colgroup> now u can use |
Beta Was this translation helpful? Give feedback.
-
I spent a few hours today diving into this myself. My constraints that it had to work with column resizing (specifically, the performant guide, which we do need to avoid render flickers). I think I'd like to take time to port what I've done into an example to share, but will give an overview to see if there is interest. Goal 1. I want to be able to resize columns , and define max/min widths to be respected This means we need a table aware both of resizing considerations and flex considerations. In the resizing example, CSS vars are used to performantly update. For my purposes, I decided to implement a However, when resizing, as some have noted with styling-focused solutions, the table "jumps". Or, the styling overrides "break" resizing. To fix this, I took it a step further. When a resize is starting, I delay triggering the resize handler while i use a ref to determine the rendered width (the flexed width) of the column, and call During and after resizing, relying on the flex width for a column is no longer tenable. As a result, the I abstracted this into a hook for my use case. I can post it / create an example if anyone else could benefit from this solution. |
Beta Was this translation helpful? Give feedback.
-
For those still interested, the way I solved this was by calculating the default size of the columns you don't want to be locked and using an empty "filler" column that is set to auto. Essentially all the other columns are acting as auto but in order to make it work properly with TanStack you need to provide the exact width it has, when the columns are set to auto through CSS it doesn't know the size of the column and therefor jumps to the default of
I haven't seen any real solution that doesn't use some sort of filler column, but that would be the main caveat of this and why it's not as pretty as you'd want it to be. Otherwise it did achieve the behavior I was looking for |
Beta Was this translation helpful? Give feedback.
-
In my case, this helps. <th
...
style={{
minWidth: header.column.columnDef.size,
maxWidth: header.column.columnDef.size,
}}
> <td
...
style={{
minWidth: cell.column.columnDef.size,
maxWidth: cell.column.columnDef.size,
}}
> If you are using shadcn, const modelColumns: ColumnDef<Model>[] = [
{
accessorKey: 'model.name',
header: 'Name',
size: 100,
},
{
accessorKey: 'model.description',
header: 'Description',
size: 400,
},
] <TableHead
...
style={{
minWidth: header.column.columnDef.size,
maxWidth: header.column.columnDef.size,
}}
> <TableCell
...
style={{
minWidth: cell.column.columnDef.size,
maxWidth: cell.column.columnDef.size,
}}
> |
Beta Was this translation helpful? Give feedback.
-
On v8, you can do something like this if you're also following the performant column resizing example: Function to calculate column sizes (same output as the one from example, but just using reduce instead of for loop) const columnSizeVars = useMemo(() => {
return table
.getFlatHeaders()
.reduce<Record<string, number>>((colSizes, header) => {
colSizes[`--header-${header.id}-size`] = header.getSize();
colSizes[`--col-${header.column.id}-size`] = header.column.getSize();
return colSizes;
}, {});
}, [table.getState().columnSizingInfo, table.getState().columnSizing]); In your style={{
width: header.column.getCanResize()
? 'auto' // if resizable, min-width & max-width will set the width, while auto will handle scaling
: `calc(var(--header-${header?.id}-size) * 1px)`, // if not resizable, set width to the header size
minWidth: `calc(var(--header-${header?.id}-size) * 1px)`,
maxWidth: `calc(var(--header-${header?.id}-size) * 1px)`,
}} And similarly, in your style={{
width: cell.column.getCanResize()
? 'auto' // if resizable, min-width & max-width will set the width, while auto will handle scaling
: `calc(var(--col-${cell.column.id}-size) * 1px)`, // if not resizable, set width to the header size
minWidth: `calc(var(--col-${cell.column.id}-size) * 1px)`,
maxWidth: `calc(var(--col-${cell.column.id}-size) * 1px)`,
}} Whatever column you want to be fixed size, you set Note
Tip
Important
Warning
|
Beta Was this translation helpful? Give feedback.
-
i have hax to handle width col, you can make function to handle inline css, like this |
Beta Was this translation helpful? Give feedback.
-
If you don't have to deal with dynamic resizing and looking for a way to create a fixed table layout & to set fixed sizes for some columns (eh), you can use colspan attribute, its a hack but just works First add rows.map(row => (
<RowComponent>
<CellComponent colspan={row.id === "select" ? 1 : 2} />
</RowComponent>
)) This way we are replacing every cell's width with their 2x values, except cell "select" thus new select cell will be 0.5x sized relative to other cells |
Beta Was this translation helpful? Give feedback.
-
Not sure if this helps... It gave me the flexibility I was looking for on any column. I am using 8.20.5
and and finally the table columnDef
|
Beta Was this translation helpful? Give feedback.
The solution I found is to change the default column width property to auto, and override the default width of 150.
See: