Ability to set column width and className
for <th> and <td> in shadcn-ui Data Table
#1122
-
Hi, I am unable to add classNames to and in shadcn-ui Data Table, I used the same method as used on Data Table - shadcn/ui but I am unable to set classNames in the above stated elements |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
I want to do something like this
|
Beta Was this translation helpful? Give feedback.
-
I copied and pasted this snippet into VSCode: I think you should be able to target and tweak the header row (to include a All this to say, you're going about this the wrong way. The But you can rewrite the markup completely and use things like Hope this helps. If you make a CodeSandbox or something, I'll be happy to tweak it to work for you. |
Beta Was this translation helpful? Give feedback.
-
I found that you can actually pass any property you want to the columns definitions If you put a custom atribute like Then you can put that The only problem was extending the type definitions for Here is a workaround I glued with help of AI, though I don't think this is the cleanest way to do it: definitions.ts import type { ColumnDef } from "@tanstack/react-table";
export type CustomColumnDef<TData, TValue> = ColumnDef<TData, TValue> & {
headClassName?: string;
}; columns.tsx import type { CustomColumnDef } from "@/lib/definitions";
export const columns: CustomColumnDef<Todo, unknown>[] = [
{
id: "actions",
headClassName: "w-[80px]",
header: "Actions",
},
]; data-table.tsx import type { CustomColumnDef } from "@/lib/definitions";
interface DataTableProps<TData, TValue> {
columns: CustomColumnDef<TData, TValue>[];
data: TData[];
}
export function DataTable<TData, TValue>({
columns,
data,
}: DataTableProps<TData, TValue>) {
....
return (
...
<TableHeader>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id}>
{headerGroup.headers.map((header) => {
const headClassName = (
header.column.columnDef as CustomColumnDef<unknown, unknown>
).headClassName;
return (
<TableHead className={headClassName} key={header.id}>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext(),
)}
</TableHead>
);
})}
</TableRow>
))}
</TableHeader>
...
);
} It manages to prevent typescript from complaining but I'm not certain if the |
Beta Was this translation helpful? Give feedback.
Hi, just found a workaround, added an array of classNames as a prop in data-table.tsx, and took the column index from the map in data-table.tsx, now I just pass the classNames like
["w-32", "w-48", "", "w-12", "w-32", "w-16"]
, works fine for me, thanks for the helpThis discussion can be hence closed now
Thanks everyone for making such a beautiful library :D