Basic column visibility
Pass withColumnVisibility to useDataGrid to let users show and hide columns.
DataGrid.ColumnVisibilityToggle renders a button with a menu of columns, DataGrid.Toolbar
without children renders it automatically:
First Name | Last Name | Email | Department | Salary | Status |
|---|---|---|---|---|---|
| Alice | Johnson | alice.johnson@example.com | Engineering | 95000 | active |
| Bob | Smith | bob.smith@example.com | Marketing | 72000 | active |
| Carol | Williams | carol.williams@example.com | Sales | 68000 | active |
| David | Brown | david.brown@example.com | HR | 65000 | inactive |
| Eva | Davis | eva.davis@example.com | Finance | 88000 | active |
| Frank | Miller | frank.miller@example.com | Engineering | 102000 | active |
| Grace | Wilson | grace.wilson@example.com | Marketing | 76000 | active |
| Henry | Taylor | henry.taylor@example.com | Sales | 71000 | inactive |
| Irene | Anderson | irene.anderson@example.com | Engineering | 97000 | active |
| James | Thomas | james.thomas@example.com | Finance | 91000 | active |
| Karen | Jackson | karen.jackson@example.com | HR | 63000 | active |
| Leo | White | leo.white@example.com | Engineering | 110000 | active |
| Mia | Harris | mia.harris@example.com | Sales | 74000 | active |
| Nathan | Martin | nathan.martin@example.com | Marketing | 69000 | inactive |
| Olivia | Garcia | olivia.garcia@example.com | Finance | 85000 | active |
| Patrick | Martinez | patrick.martinez@example.com | Engineering | 99000 | active |
| Quinn | Robinson | quinn.robinson@example.com | HR | 67000 | active |
| Rachel | Clark | rachel.clark@example.com | Sales | 73000 | active |
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { employees } from './data';
function Demo() {
const store = useDataGrid({
data: employees,
columns: [
{ accessorKey: 'firstName', header: 'First Name' },
{ accessorKey: 'lastName', header: 'Last Name' },
{ accessorKey: 'email', header: 'Email', size: 220 },
{ accessorKey: 'department', header: 'Department' },
{ accessorKey: 'salary', header: 'Salary' },
{ accessorKey: 'status', header: 'Status' },
],
withColumnVisibility: true,
});
return (
<DataGrid store={store}>
<DataGrid.Toolbar>
<DataGrid.ColumnVisibilityToggle />
</DataGrid.Toolbar>
</DataGrid>
);
}Controlled visibility state
Pass columnVisibility and onColumnVisibilityChange to useDataGrid for controlled state.
The state is an object of column ids and boolean values:
First Name | Last Name | Email | Department | Salary | Status |
|---|---|---|---|---|---|
| Alice | Johnson | alice.johnson@example.com | Engineering | 95000 | active |
| Bob | Smith | bob.smith@example.com | Marketing | 72000 | active |
| Carol | Williams | carol.williams@example.com | Sales | 68000 | active |
| David | Brown | david.brown@example.com | HR | 65000 | inactive |
| Eva | Davis | eva.davis@example.com | Finance | 88000 | active |
| Frank | Miller | frank.miller@example.com | Engineering | 102000 | active |
| Grace | Wilson | grace.wilson@example.com | Marketing | 76000 | active |
| Henry | Taylor | henry.taylor@example.com | Sales | 71000 | inactive |
| Irene | Anderson | irene.anderson@example.com | Engineering | 97000 | active |
| James | Thomas | james.thomas@example.com | Finance | 91000 | active |
| Karen | Jackson | karen.jackson@example.com | HR | 63000 | active |
| Leo | White | leo.white@example.com | Engineering | 110000 | active |
| Mia | Harris | mia.harris@example.com | Sales | 74000 | active |
| Nathan | Martin | nathan.martin@example.com | Marketing | 69000 | inactive |
| Olivia | Garcia | olivia.garcia@example.com | Finance | 85000 | active |
| Patrick | Martinez | patrick.martinez@example.com | Engineering | 99000 | active |
| Quinn | Robinson | quinn.robinson@example.com | HR | 67000 | active |
| Rachel | Clark | rachel.clark@example.com | Sales | 73000 | active |
import { useState } from 'react';
import { ColumnVisibilityState } from '@tanstack/react-table';
import { Checkbox, Group } from '@mantine/core';
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { employees } from './data';
const allColumns = ['firstName', 'lastName', 'email', 'department', 'salary', 'status'];
function Demo() {
const [columnVisibility, setColumnVisibility] = useState<ColumnVisibilityState>({});
const store = useDataGrid({
data: employees,
columns: [
{ accessorKey: 'firstName', header: 'First Name' },
{ accessorKey: 'lastName', header: 'Last Name' },
{ accessorKey: 'email', header: 'Email', size: 220 },
{ accessorKey: 'department', header: 'Department' },
{ accessorKey: 'salary', header: 'Salary' },
{ accessorKey: 'status', header: 'Status' },
],
columnVisibility,
onColumnVisibilityChange: setColumnVisibility,
});
const visibleColumns = allColumns.filter((col) => columnVisibility[col] !== false);
return (
<>
<Checkbox.Group value={visibleColumns} onChange={(value) => {
const next: ColumnVisibilityState = {};
allColumns.forEach((col) => {
next[col] = value.includes(col);
});
setColumnVisibility(next);
}} mb="md">
<Group>
{allColumns.map((col) => (
<Checkbox key={col} value={col} label={col} />
))}
</Group>
</Checkbox.Group>
<DataGrid store={store} />
</>
);
}Disable hiding per column
Set enableHiding: false on a column definition to prevent the column from being hidden.
Such columns are not listed in the visibility menu:
First Name | Last Name | Email | Department | Salary | Status |
|---|---|---|---|---|---|
| Alice | Johnson | alice.johnson@example.com | Engineering | 95000 | active |
| Bob | Smith | bob.smith@example.com | Marketing | 72000 | active |
| Carol | Williams | carol.williams@example.com | Sales | 68000 | active |
| David | Brown | david.brown@example.com | HR | 65000 | inactive |
| Eva | Davis | eva.davis@example.com | Finance | 88000 | active |
| Frank | Miller | frank.miller@example.com | Engineering | 102000 | active |
| Grace | Wilson | grace.wilson@example.com | Marketing | 76000 | active |
| Henry | Taylor | henry.taylor@example.com | Sales | 71000 | inactive |
| Irene | Anderson | irene.anderson@example.com | Engineering | 97000 | active |
| James | Thomas | james.thomas@example.com | Finance | 91000 | active |
| Karen | Jackson | karen.jackson@example.com | HR | 63000 | active |
| Leo | White | leo.white@example.com | Engineering | 110000 | active |
| Mia | Harris | mia.harris@example.com | Sales | 74000 | active |
| Nathan | Martin | nathan.martin@example.com | Marketing | 69000 | inactive |
| Olivia | Garcia | olivia.garcia@example.com | Finance | 85000 | active |
| Patrick | Martinez | patrick.martinez@example.com | Engineering | 99000 | active |
| Quinn | Robinson | quinn.robinson@example.com | HR | 67000 | active |
| Rachel | Clark | rachel.clark@example.com | Sales | 73000 | active |
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { employees } from './data';
function Demo() {
const store = useDataGrid({
data: employees,
columns: [
{ accessorKey: 'firstName', header: 'First Name', enableHiding: false },
{ accessorKey: 'lastName', header: 'Last Name', enableHiding: false },
{ accessorKey: 'email', header: 'Email', size: 220 },
{ accessorKey: 'department', header: 'Department' },
{ accessorKey: 'salary', header: 'Salary' },
{ accessorKey: 'status', header: 'Status' },
],
withColumnVisibility: true,
});
return (
<DataGrid store={store}>
<DataGrid.Toolbar>
<DataGrid.ColumnVisibilityToggle />
</DataGrid.Toolbar>
</DataGrid>
);
}Column menu
With withColumnMenu on DataGrid, every hideable column gets a Hide column item in its
header menu, see Customization.
Toggle all columns
Use the table instance to show or hide all columns:
const store = useDataGrid({ data, columns, withColumnVisibility: true });
// Hide all columns
store.table.toggleAllColumnsVisible(false);
// Show all columns
store.table.toggleAllColumnsVisible(true);Programmatic visibility
Use the column instance to show or hide one column:
// Toggle a specific column
store.table.getColumn('email')?.toggleVisibility();
// Set visibility explicitly
store.table.getColumn('email')?.toggleVisibility(false);Initial hidden columns
Pass columnVisibility to useDataGrid to hide columns initially:
const store = useDataGrid({
data,
columns,
columnVisibility: { email: false, startDate: false },
});