Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Data Table examples · Standard

Data Table with Column Visibility for React

A table of orders with a Columns menu that shows and hides each column.

Framework
UI library

Four orders in six columns and a Columns menu above the table with a checked item per column. Uncheck one and the column leaves the header and every row. TanStack Table's column visibility keeps the state; the menu is our Dropdown Menu. Use it for wide tables where people choose what they need to see.

Preview

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

FAQ

Where is column visibility stored?

In the table, from columnVisibilityFeature; every column starts visible. Pass initialState: { columnVisibility: { date: false } } to start with a column hidden.

How does the Columns menu toggle a column?

It lists table.getAllColumns() filtered by getCanHide(), checked from column.getIsVisible(). Each DropdownMenuCheckboxItem calls column.toggleVisibility from onCheckedChange.

What text does each item show?

The column's id with capitalize: Id, Customer, Product, Quantity, Total and Date. Give a column meta with a label, or map ids to names, to show something else.

What does the trigger look like?

An outline Button reading Columns with a chevron after the text. The menu opens below it, aligned to its end.

Does a refresh keep hidden columns hidden?

No. Visibility lives in the table's state. Save it from onColumnVisibilityChange and pass it back as initial state to keep it.

Can every column be hidden?

Yes, nothing stops the last one. Set enableHiding: false on a column, such as the order id, to keep it in the table and out of the menu.