[column-visibility] How to display the header of a column instead of the column.id?
#4857
Replies: 4 comments 8 replies
-
|
Just don't use column.id. use column.columnDef.header |
Beta Was this translation helpful? Give feedback.
-
|
as @IudexSoren said, you can use Then you can access it: |
Beta Was this translation helpful? Give feedback.
-
|
I ran into the same problem and ended-up using flexRender(column.columnDef.header, {
table,
column,
header: {column} as Header<unknown, unknown>,
})You can replace Since we don't have direct access to |
Beta Was this translation helpful? Give feedback.
-
|
I found another workaround for the issue. While debugging, I noticed you can actually use: Here’s the full example: function getColumnLabel<TData>(column: Column<TData, unknown>): string {
if (typeof column.columnDef.header === 'function') {
const headerNode = column.columnDef.header({ column } as HeaderContext<
TData,
unknown
>)
return headerNode?.props?.name ?? column.id
}
return column.id
}
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="outline" className="ml-auto">
Columns <ChevronDown />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
{table
.getAllColumns()
.filter((column) => column.getCanHide())
.map((column) => {
return (
<DropdownMenuCheckboxItem
key={column.id}
checked={column.getIsVisible()}
onCheckedChange={(value) =>
column.toggleVisibility(!!value)
}
>
{getColumnLabel(column)}
</DropdownMenuCheckboxItem>
)
})}
</DropdownMenuContent>
</DropdownMenu> |
Beta Was this translation helpful? Give feedback.


Uh oh!
There was an error while loading. Please reload this page.
-
Looking at https://tanstack.com/table/v8/docs/examples/react/column-visibility
the box to show the columns to toggle uses:
and in particular
column.idto display the label.But how to use actual header value here? E.g. in this example
Last Nameinstead oflastName...Beta Was this translation helpful? Give feedback.
All reactions