table icon indicating copy to clipboard operation
table copied to clipboard

Size property not working

Open anduscheung opened this issue 1 year ago • 21 comments

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

  • [X] I agree to follow this project's Code of Conduct
  • [X] 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.

anduscheung avatar Oct 05 '23 08:10 anduscheung

Seeing this as well

aoloo avatar Oct 11 '23 15:10 aoloo

@anduscheung , @aoloo

You're probably missing the part where you set the width by yourself in the styles prop:

<td
  style={{
    width: cell.column.getSize(),
  }}
>
    ...
</td>

It's shown here: Tanstack Column Sizing

sergor5 avatar Oct 26 '23 06:10 sergor5

Thanks, I thought it is an automatic property 👍

anduscheung avatar Dec 02 '23 07:12 anduscheung

Someone know a workaround? still doesn't works

dawalberto avatar Jan 26 '24 12:01 dawalberto

Someone know a workaround? still doesn't works

Why do you need "a workaround"? You probably have just not hooked up the column size APIs to your CSS properly. Follow the official examples and you should be fine.

KevinVandy avatar Jan 26 '24 22:01 KevinVandy

@KevinVandy this is my column helper:

...
columnHelper.accessor('ref', {
	id: 'ref',
	cell: (info) => <span className='whitespace-nowrap'>{info.renderValue()}</span>,
	size: 300,
	header: 'ID',
	footer: (info) => info.column.id,
}),
...

And this my table:

...
<td
	key={cell.id}
	style={{ width: cell.column.getSize() }}
...

Am I missing something?

dawalberto avatar Jan 29 '24 06:01 dawalberto

Just also apply the width in th header elements

KevinVandy avatar Jan 29 '24 07:01 KevinVandy

@KevinVandy I already did but still not working 🤷‍♂️

...
<tr key={headerGroup.id}>
	{headerGroup.headers.map((header) => (
		<th
			key={header.id}
			colSpan={header.colSpan}
			style={{ position: 'relative', width: header.getSize() }}
		>
...

dawalberto avatar Jan 29 '24 09:01 dawalberto

Ok. Share a full sandbox showing it not working and we can go from there.

KevinVandy avatar Jan 29 '24 14:01 KevinVandy

@KevinVandy @dawalberto any fix to this?

birddigital avatar Feb 15 '24 17:02 birddigital

Someone share a sandbox showing it not working. As far as we can tell, everything in TanStack Table is working perfectly fine in regards to column sizing being applied correctly per column. Make sure you are always using pixel units.

KevinVandy avatar Feb 15 '24 17:02 KevinVandy

I'm using shadcn-ui v0.8.0 and in the TableHeader components I used header.column.columnDef.meta?.width

<TableHeader>
            {table.getHeaderGroups().map((headerGroup) => (
              <TableRow key={headerGroup.id}>
                {headerGroup.headers.map((header) => {
                  return (
                    <TableHead key={header.id} style={{ width: header.column.columnDef.meta?.width }}>
                      {header.isPlaceholder ? null : flexRender(header.column.columnDef.header, header.getContext())}
                    </TableHead>
                  );
                })}
              </TableRow>
            ))}
          </TableHeader>
<TableHeader>
            {table.getHeaderGroups().map((headerGroup) => (
              <TableRow key={headerGroup.id}>
                {headerGroup.headers.map((header) => {
                  return (
                    <TableHead key={header.id} style={{ width: header.column.columnDef.meta?.width }}>
                      {header.isPlaceholder ? null : flexRender(header.column.columnDef.header, header.getContext())}
                    </TableHead>
                  );
                })}
              </TableRow>
            ))}
          </TableHeader>

Then in my column definition i used the metadata object with the width value. Not sure if this is proper but it worked.

{
    accessorKey: 'isResold',
    meta: {
      width: 72,
    },
    header: () => {
      return <div className="flex justify-center">Resold</div>;
    },
    cell: ({ row }) => {
      switch (row.original.isResold) {
        case true:
          return (
            <div className="flex justify-center items-center ">
              <CheckCircledIcon className="w-4 h-4 text-green-500" />
            </div>
          );
        case false:
          return (
            <div className="flex justify-center items-center ">
              <CrossCircledIcon className="w-4 h-4 text-slate-500" />
            </div>
          );
      }
    },
  }, 

Hopefully this helps.

I imagine this same approach would make the size, minSize, and maxSize work as well.

birddigital avatar Feb 15 '24 20:02 birddigital

Why would you use meta instead of the official size APIs?

KevinVandy avatar Feb 15 '24 22:02 KevinVandy

Is there an auto-fit column property? Don't want to limit Table columns to a particular width, want to expand columns based on content. I tried size: 'auto' against all columns, doesn't work. Tried some CSS workarounds(whitespace, etc), works but I lose the resize functionality Thanks in advance.

mdaffan01 avatar Feb 20 '24 08:02 mdaffan01

@KevinVandy

mdaffan01 avatar Feb 29 '24 08:02 mdaffan01

Is there an auto-fit column property? Don't want to limit Table columns to a particular width, want to expand columns based on content. I tried size: 'auto' against all columns, doesn't work. Tried some CSS workarounds(whitespace, etc), works but I lose the resize functionality Thanks in advance.

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.

KevinVandy avatar Feb 29 '24 16:02 KevinVandy

For example, here size doesn't do anything.

Oize avatar Mar 01 '24 20:03 Oize

I am using shadcn with Tanstack table and solved my issue by adding a defaultColumn prop to give columns a size. tanstack

I also needed to change the CSS in TableHead to use width : header.getSize() image

Hope it helps someone if they are using shadcn with TanStack table. :)

songexile avatar Mar 16 '24 04:03 songexile

I'm using tanstack table in shadcn/ui. Columns sizing didn't work until i add 1.

 <table // <Table/> component in shadcn
    {...{
      style: {
        width: table.getCenterTotalSize()
      }
    }}
  >
<td // <TableCell/> component in shadcn
  style={{
    width: cell.column.getSize()
  }}
>
size: 100, // in ColumnDef

Sylar-A avatar Mar 16 '24 17:03 Sylar-A

I'm using tanstack table in shadcn/ui. Columns sizing didn't work until i add 1.

 <table // <Table/> component in shadcn
    {...{
      style: {
        width: table.getCenterTotalSize()
      }
    }}
  >

This was the missing piece! Somehow the table was overriding the cell widths but this fixed it

rgorai-cc avatar Mar 28 '24 14:03 rgorai-cc

I'm using tanstack table in shadcn/ui. Columns sizing didn't work until i add 1.

 <table // <Table/> component in shadcn
    {...{
      style: {
        width: table.getCenterTotalSize()
      }
    }}
  >
<td // <TableCell/> component in shadcn
  style={{
    width: cell.column.getSize()
  }}
>
size: 100, // in ColumnDef

Worked for Chakra UI too.

nerixim avatar Apr 01 '24 01:04 nerixim

columnHelper.accessor('firstName', {
  cell: info => info.getValue(),
  footer: info => info.column.id,
  size: 500,
  maxSize: 700, // how about this line?
}), 

every answer here is working with size, but not with maxSize. Have someone any workaround with this?

MDziurakh avatar May 31 '24 08:05 MDziurakh

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.

KevinVandy avatar Jun 03 '24 20:06 KevinVandy

What if I just want to add width to one column?

[
   {
      ...
      accessorKey: 'id',
      width: 75,
   },
   {
      ...
      accessorKey: 'color',
   },
   ...
]

the getSize function returns the default (150) in every other column

<th style=`75px`>...</th>
<th style=`150px`>...</th>

and I want them to remain unset

Raphaww avatar Jun 24 '24 17:06 Raphaww

@Raphaww, how about this implementation?

width: header.getSize() !== 150 ? header.getSize() : undefined

image

MDziurakh avatar Jun 24 '24 17:06 MDziurakh

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 defaultWidth or 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 +1

const minSize = 50
useReactTable({ ..., defaultColumn: {
   size: minSize + 1,
   minSize,
}})

<th
{...(header.getSize() !== (minSize + 1) && { style: { width: `${header.getSize()}px` } })}
/>

That 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 avatar Jun 24 '24 18:06 Raphaww