The ColumnVisibilityPanel provides an interactive list for users to toggle the visibility of specific columns within the grid. This is essential for managing dense datasets where only a subset of information is needed at once.
- Auto-Generating List: It automatically builds a list of all togglable columns from the grid's state.
- Searchable Interface: Includes a quick search to find specific columns in grids with many fields.
- Toggle State: Synchronizes instantly with the grid's
columnVisibilityModel. - Exclusion Logic: Lists only the columns it is given, leaving out those marked
hideable: false(unlessshowNonHideableColumns). The grid's system columns (checkbox, expand, drag handle) are not columns, and its row-grouping__group__column ishideable: false, so neither appears. - Accessible names: Each checkbox is labelled with its column's
headerName(orfield), and the footer checkbox with "Show/Hide All"; clicking the name toggles the column.
The grid's toolbar and header menu already include this panel. The column menu's Manage columns opens the toolbar's Columns panel when slots.toolbar renders a GridToolbar that receives the grid's slot props; otherwise (no toolbar, or a custom toolbar without GridToolbar) it opens a standalone panel at the grid's top-right corner. Both close on Escape or a click outside. Import it yourself (exported since v2.1) only when you want the list somewhere else, such as a sidebar or modal. It is a controlled component: you own the visibility model and pass it in.
import { useMemo, useState } from 'react';
import { DataGrid, ColumnVisibilityPanel } from '@opencorestack/opengridx';
import type { GridColDef } from '@opencorestack/opengridx';
function Report({ rows, columns }: { rows: Row[]; columns: GridColDef<Row>[] }) {
const [visibility, setVisibility] = useState<Record<string, boolean>>({});
const visibleColumns = useMemo(
() => new Set(columns.filter(c => visibility[c.field] !== false).map(c => c.field)),
[columns, visibility],
);
const setAll = (visible: boolean) =>
setVisibility(Object.fromEntries(columns.filter(c => c.hideable !== false).map(c => [c.field, visible])));
return (
<div style={{ display: 'flex' }}>
<aside className="my-column-sidebar">
<ColumnVisibilityPanel
columns={columns}
visibleColumns={visibleColumns}
onVisibilityChange={(field, isVisible) => setVisibility(v => ({ ...v, [field]: isVisible }))}
onShowAll={() => setAll(true)}
onHideAll={() => setAll(false)}
/>
</aside>
<DataGrid rows={rows} columns={columns} columnVisibilityModel={visibility} onColumnVisibilityModelChange={setVisibility} />
</div>
);
}| Prop | Type | Description |
|---|---|---|
columns |
GridColDef[] |
Columns to list |
visibleColumns |
Set<string> |
Fields currently visible |
onVisibilityChange |
(field, isVisible) => void |
Toggle one column |
onShowAll / onHideAll |
() => void |
Bulk actions (only hideable columns are affected) |
onColumnReorder |
(fromField, toField) => void |
Optional — enables drag-to-reorder in the list |
onColumnOrderReset |
() => void |
Optional — shows a reset-order action |
showNonHideableColumns |
boolean |
Show hideable: false columns as disabled rows. Default false |
- Column Resolution: The panel lists the
columnsit receives, in that order (the grid passes its current column order). - Filtration: It leaves out columns marked
hideable: false, unlessshowNonHideableColumnsis set (then they are shown as disabled rows). There is no filtering by field name: a field starting with__is listed like any other. - State Management: Toggling a checkbox calls
onVisibilityChange(field, isVisible); inside the grid that updatescolumnVisibilityModeland firesonColumnVisibilityModelChange.
The built-in panel cannot be replaced through the slots API. To drive visibility from outside, use the controlled props, optionally together with a standalone ColumnVisibilityPanel as shown above:
<DataGrid
columnVisibilityModel={model}
onColumnVisibilityModelChange={setModel}
/>- Logical Grouping: Ensure your columns have clear, human-readable
headerNamevalues so they are easily identifiable in the list. - Persistent State: Use
onColumnVisibilityModelChangeto save column preferences to local storage or a database so the user's layout is preserved.