Next.js Discord

Discord Forum

Adding fixed widths to shad/cn Data Table columns

Unanswered
Lithuanian Hound posted this in #help-forum
Open in Discord
Lithuanian HoundOP
I've intialized a basic shad/cn data table component from the docs.
How do I change the max-width of a column?

I've tried placing a max-w-[100px] on both the header div and the cell div in the columns.tsx file,
but as seen in the first image, the column cell itself is shrunk to 100px but the actual <th> or <td> itself is unaffected.

The second image is what I would like to happen - the amount column being 100px regardless of screensize.


 
export const columns: ColumnDef<Payment>[] = [
 ...
  {
    accessorKey: "amount",
    header: ({ column }) => {
      return (
        <div className="flex max-w-[125px] justify-end">
          <Button
            variant="ghost"
            onClick={() => column.toggleSorting(column.getIsSorted() === "asc")}
          >
            Amount
            {column.getIsSorted() === "asc" ? (
              <ArrowUpIcon className="ml-2 h-3 w-3" />
            ) : (
              <ArrowDownIcon className="ml-2 h-3 w-3" />
            )}
          </Button>
        </div>
      )
    },
    cell: ({ row }) => {
      const { amount } = row.original
      const formatted = new Intl.NumberFormat("en-US", {
        style: "currency",
        currency: "USD",
      }).format(amount)

      return (
        <div className="max-w-[125px] px-4 text-right font-medium">{formatted}</div>
      )
    },
  },
]

5 Replies

Asari
I actually went through this today as well (using TailwindUI but should be very similar). What you (and I) are trying to do with tailwind doesn't affect table tags the same way as normal divs in tailwind. Putting what you want in you style={width...} instead of the className should do the trick (unless Shad uses something else other than a table under the hood, which idk why that would be a thing)
@Asari I actually went through this today as well (using TailwindUI but should be very similar). What you (and I) are trying to do with tailwind doesn't affect table tags the same way as normal divs in tailwind. Putting what you want in you style={width...} instead of the className should do the trick (unless Shad uses something else other than a table under the hood, which idk why that would be a thing)
Lithuanian HoundOP
I've applied style={width} to the header column and the cell and the same thing from the first image still happens.
It seems that the <td> is the actual container for the cells and I'm having trouble accessing that through the header and cell fields.
no idea how to target it specifically
:nyaThink:
Asari
Apply the styles to the <td>, not the header
Lithuanian HoundOP
Yeah, I'm having trouble finding out how to apply styles to that <td>. With shad/cn's data table, you can format the cells by returning some JSX for specific columns in columns.tsx, however, all that written JSX gets returned as a child of that <td> with no indication of how to target the <td>'s styles directly.

Is it even possible?
Asari
I haven't used the shad table myself so I can't speak with certainty. Under this link (https://ui.shadcn.com/docs/components/data-table) under the section "Cell Formatting", it seems you get access to how the cells are formatted. If that doesn't apply what you want, you just have to hunt down in the shad library where the <td> tags are and access that styling.