ui icon indicating copy to clipboard operation
ui copied to clipboard

Data Table ignoring column size

Open cehenriques opened this issue 1 year ago • 2 comments

Hi.

I want to set a fixed width on one of my Data Table columns, so I checked TanStack Table docs and found they have size property for the ColumnDef: https://tanstack.com/table/v8/docs/guide/column-sizing

But I tried using it and it does nothing on shadcn Data Table. It seems the size properties are not being used on shadcn Data Table:

<TableCell key={cell.id}>
  {flexRender(cell.column.columnDef.cell, cell.getContext())}
</TableCell>

if I pass the size to the width property of the cell it works:

<TableCell key={cell.id} width={cell.column.columnDef.size}>
  {flexRender(cell.column.columnDef.cell, cell.getContext())}
</TableCell>

cehenriques avatar Feb 25 '24 16:02 cehenriques

I think you need to hook this up yourself.

https://tanstack.com/table/v8/docs/guide/column-sizing#column-size-apis

How you apply these size styles to your markup is up to you, but it is pretty common to use either CSS variables or inline styles to apply the column sizes.

<th
  key={header.id}
  colSpan={header.colSpan}
  style={{ width: `${header.getSize()}px` }}
>

kevinmitch14 avatar Feb 27 '24 16:02 kevinmitch14

this solution works for user-defined width but the issue is it will always convert the width into px values. The default column width is 150. which is ok because it renders a relative width between all columns. but with the above code the relative width converts to fixed width of 150px for all columns. even the ones where only icons are present.

One solution could be to not apply the width style if the width === default-width (150 in our case).

But this means a user will never get 150px width...

mshahzebraza avatar Apr 26 '24 10:04 mshahzebraza

Hi. I tried to resolve this too, but still getting wrong column width in first column of table. Hope "shadcn/ui" will provide a solution for this issue.

image

xspaceboy avatar May 21 '24 20:05 xspaceboy

I've been experimenting and now this is working for me, using tailwind classes on the header aswell as the cell if you think it could exceed the header's width.

{ accessorKey: "title", header: () => ( <div className="text-right w-10">Title</div> ), cell: ({ row }) => ( <div className="flex gap-2 items-center"> <span className="truncate w-10 font-medium"> {row.getValue("title")} </span> </div> ) },

nuclei272 avatar May 23 '24 22:05 nuclei272

This issue has been automatically closed because it received no activity for a while. If you think it was closed by accident, please leave a comment. Thank you.

shadcn avatar Jun 15 '24 23:06 shadcn

Has anyone found a trick? I'm having trouble customizing the column sizes.

fport avatar Jun 21 '24 16:06 fport

@fport Try this.

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,
  }}
>

devnjw avatar Jun 26 '24 15:06 devnjw

Hi.

I want to set a fixed width on one of my Data Table columns, so I checked TanStack Table docs and found they have size property for the ColumnDef: https://tanstack.com/table/v8/docs/guide/column-sizing

But I tried using it and it does nothing on shadcn Data Table. It seems the size properties are not being used on shadcn Data Table:

<TableCell key={cell.id}>
  {flexRender(cell.column.columnDef.cell, cell.getContext())}
</TableCell>

if I pass the size to the width property of the cell it works:

<TableCell key={cell.id} width={cell.column.columnDef.size}>
  {flexRender(cell.column.columnDef.cell, cell.getContext())}
</TableCell>

Great! but I have a question. How to handle table header width?

cupid20103 avatar Jul 04 '24 09:07 cupid20103

hi guys, im not sure i get correct answer for that, but for me work use block class to apply styles in cell

Kamil-Drozdz avatar Aug 02 '24 19:08 Kamil-Drozdz

After some research, I found a nice workaround using ColumnMeta that perfectly works with shadcn data-table.

// react-table.d.ts
import "@tanstack/react-table";

declare module "@tanstack/react-table" {
  interface ColumnMeta {
    headerClassName?: string;
    cellClassName?: string;
  }
}

See full snippet: https://gist.github.com/mxkaske/4f87a26bbba3486d2f2c0c83f60163eb

mxkaske avatar Aug 02 '24 19:08 mxkaske