DocsGuides

Guides

Column Hiding Guide

How to use, customize, or disable column hiding and visibility features of Shadcn Table

The column hiding feature is enabled by default and allows the user to hide data columns from either the column menu (Hide) or the columns menu. The columns menu opens from the Columns button, which is pinned to the toolbar by default (aria-label "Show/Hide columns"), and from the Columns row of the view settings panel. Unpin it in the panel to keep it only there.

Example

Relevant Table Options

Prop NameTypeDescription
enableHidingboolean
onColumnVisibilityChangeOnChangeFn<ColumnVisibilityState>If provided, this function will be called with an updaterFn when state.columnVisibility changes. This overrides the default internal state management, so you will need to persist the state change either fully or partially outside of the table.

Relevant Column Options

Column OptionTypeDescription
enableHidingboolean
visibleInShowHideMenubooleanSet to false if you want to hide a column from the show/hide menu.

Relevant State

State OptionTypeDescription
columnVisibilityRecord<string, boolean>an object mapping column IDs to a boolean indicating whether the column is currently visible

Hide Some Columns by Default

You can easily hide columns by default by setting the columnVisibility state or initialState to hide the desired columns by id.

const table = useShadcnTable({
  columns,
  data,
  initialState: { columnVisibility: { firstName: false } }, //hide firstName column by default
});

return <ShadcnTable table={table} />;

Dynamically Hide Columns

If you need to control how the columns are hidden dynamically, you can use state instead of initialState along with the onColumnVisibilityChange table option. Or alternatively you can use the setColumnVisibility table instance api.

const [columnVisibility, setColumnVisibility] = useState({
  firstName: false,
});

useEffect(() => {
  setColumnVisibility({ firstName: true }); //programmatically show firstName column
}, [someDependency]);

const table = useShadcnTable({
  columns,
  data,
  state: { columnVisibility }, //manage columnVisibility state
  onColumnVisibilityChange: setColumnVisibility,
});

//NOTE: Instead of managing the columnVisibility state yourself, we could call a table instance api
table.setColumnVisibility({ firstName: true }); //programmatically show firstName column

Disable Column Hiding

If you do not want this feature to be enabled at all, you can disable it by setting the enableHiding table option to false.

const table = useShadcnTable({
  columns,
  data,
  enableHiding: false,
});

Alternatively, you can disable hiding specific columns by setting the enableHiding column option to false per column.

If you want to hide certain columns by default, you can specify column visibility in the initialState.columnVisibility table option.

Storybook

Enable Column Hiding on Display Columns

By default, column hiding is only enabled on data columns. Display columns, such as st-row-numbers, st-row-select, etc., do not have column hiding enabled, and their toggle will be disabled. You can turn that back on by setting the enableHiding option to true in the displayColumnsOptions table option.

const table = useShadcnTable({
  columns,
  data,
  displayColumnDefOptions: {
    'st-row-numbers': {
      enableHiding: true, //now row numbers are hidable too
    },
  },
});

See the Display Columns Feature Guide for a more in depth explanation of the displayColumnsOptions table option.

Hide Column From Show Hide Menu

By default, all columns are listed in the columns menu that opens from the pinned Columns button in the toolbar and from the Columns row of the view settings panel. You can hide a column from this menu by setting the visibleInShowHideMenu column option to false.

const columns = [
  {
    accessorKey: 'uuid',
    header: 'UUID',
    visibleInShowHideMenu: false, //hide this column from the show hide menu, but still show the column in the table
  },
];

const table = useShadcnTable({
  columns,
  data,
  displayColumnDefOptions: {
    'st-row-actions': {
      visibleInShowHideMenu: false, //hide the built-in row actions column from the show hide menu
    },
  },
});

Custom Columns Menu

The ST_ShowHideColumnsMenu component is one of the few ST components that is pretty opinionated and not easily customizable. Instead of trying to customize the menu with overrides, it might be easier for you to just build your own new button and menu from scratch using the Table and Column Instance APIs.

Adding your own Toolbar Buttons is covered in the Toolbar Guide

To open the stock menu from a button of your own, pass the button as the menu's trigger. A press opens the menu and a second press closes it; open and onOpenChange control it.

import { Button } from '@/components/ui/button';
import { ST_ShowHideColumnsMenu } from '@/components/ui/shadcn-table';

<ST_ShowHideColumnsMenu trigger={<Button variant="outline">Columns</Button>} />;

The menu reads the table from context, so render it inside the table's toolbar (for example from renderToolbarInternalActions) or inside an ST_TableProvider.

If all you want to do is customize the buttons above the columns menu, you can import and use the ST_ShowHideColumnsMenuItems component from shadcn table, which is a component that renders the columns in the list with the toggle switches, but then render your own buttons in the top or bottom of the menu itself.

View Extra Storybook Examples

On this page