Examples › Column resizing

Mantine DataTable lets users resize columns by dragging a handle at the edge of a column header. Resized widths can optionally be persisted in the browser’s local storage, so they survive page reloads.

Name
Street address
City
State
Mission statement
Feest, Bogan and Herzog21716 Ratke DriveStromanportWYInnovate bricks-and-clicks metrics.
Cummerata - Kuhlman6389 Dicki StreamSouth GateNHHarness real-time channels.
Goyette Inc8873 Mertz RapidDorthysideIDProductize front-end web services.
Runte Inc2996 Ronny MountMcAllenMAEngage synergistic infrastructures.
Goldner, Rohan and Lehner632 Broadway AvenueNorth LouieWYIncubate cross-platform metrics.
Doyle, Hodkiewicz and O'Connell576 Joyce WaysTyraburghKSScale web-enabled e-business.
Rau - O'Hara7508 Lansdowne RoadShieldsboroughMIInnovate real-time applications.
Tillman - Jacobi57918 Gwendolyn CirclesSheridanportMIMatrix viral synergies.
Connelly, Feest and Hodkiewicz7057 Stanley RoadKearaburghCAMaximize dynamic e-commerce.
Shanahan, Robel and Beier378 Berta CrescentWest GerryKSSynthesize customized portals.
Kling - McLaughlin8346 Kertzmann SquareSouth JoesphIDReinvent cross-platform channels.
Jogi - McLaughlin83462 Shazam StreetNorth JoesphIDEliminate best-of-breed e-markets.
Jogi - McLaughlin83462 Shazam StreetNorth JoesphIDEliminate best-of-breed e-markets.
No records

In order to enable column resizing with persistent widths, you’ll have to:

  • add a resizable: true property to each resizing candidate column;
  • add a storeColumnsKey: 'your_key' property to the DataTable (the column widths are persisted in local storage under this key);
  • use the useDataTableColumns() hook with the same key to get the effective columns and the resetColumnsWidth() function.
'use client';

import { DataTable, useDataTableColumns } from 'mantine-datatable';
import { Button, Group, Stack, Switch } from '@mantine/core';
import { useState } from 'react';
import { type Company, companies } from '~/data';

export default function ResizingExample() {
  const key = 'resize-example';

  const [resizable, setResizable] = useState<boolean>(true);
  const [withTableBorder, setWithTableBorder] = useState<boolean>(true);
  const [withColumnBorders, setWithColumnBorders] = useState<boolean>(true);

  const { effectiveColumns, resetColumnsWidth } = useDataTableColumns<Company>({
    key,
    columns: [
      { accessor: 'name', resizable },
      { accessor: 'streetAddress', resizable, ellipsis: true },
      { accessor: 'city', resizable, ellipsis: true },
      { accessor: 'state', resizable },
      { accessor: 'missionStatement', resizable, ellipsis: true },
    ],
  });

  return (
    <Stack>
      <DataTable
        withTableBorder={withTableBorder}
        withColumnBorders={withColumnBorders}
        storeColumnsKey={key}
        records={companies}
        columns={effectiveColumns}
      />
      <Group grow justify="space-between">
        <Group justify="flex-start">
          <Switch
            checked={resizable}
            onChange={(event) => setResizable(event.currentTarget.checked)}
            labelPosition="left"
            label="Resizable"
          />
          <Switch
            checked={withTableBorder}
            onChange={(event) => setWithTableBorder(event.currentTarget.checked)}
            labelPosition="left"
            label="Table Border"
          />
          <Switch
            checked={withColumnBorders}
            onChange={(event) => setWithColumnBorders(event.currentTarget.checked)}
            labelPosition="left"
            label="Column Borders"
          />
        </Group>
        <Group justify="right">
          <Button onClick={resetColumnsWidth}>Reset Column Width</Button>
        </Group>
      </Group>
    </Stack>
  );
}

Here’s how resizing works:

  • dragging a handle changes the width of the column and of the column next to it, so the overall table width stays the same;
  • a column can’t be made narrower than 50 pixels;
  • the last column has no resize handle, since there’s no column after it;
  • double-clicking a handle resets the two adjacent columns to their default widths;
  • calling resetColumnsWidth() restores all columns to the width they were defined with in the columns prop (or to automatic width, if they don’t have one).

Resizing without persistence

If you don’t need to remember the column widths, simply add resizable: true to the columns you want to make resizable — no storeColumnsKey or useDataTableColumns() hook required. The widths will be reset to their defaults when the component is remounted:

Name
Street address
City
State
Mission statement
Feest, Bogan and Herzog21716 Ratke DriveStromanportWYInnovate bricks-and-clicks metrics.
Cummerata - Kuhlman6389 Dicki StreamSouth GateNHHarness real-time channels.
Goyette Inc8873 Mertz RapidDorthysideIDProductize front-end web services.
Runte Inc2996 Ronny MountMcAllenMAEngage synergistic infrastructures.
Goldner, Rohan and Lehner632 Broadway AvenueNorth LouieWYIncubate cross-platform metrics.
Doyle, Hodkiewicz and O'Connell576 Joyce WaysTyraburghKSScale web-enabled e-business.
Rau - O'Hara7508 Lansdowne RoadShieldsboroughMIInnovate real-time applications.
Tillman - Jacobi57918 Gwendolyn CirclesSheridanportMIMatrix viral synergies.
Connelly, Feest and Hodkiewicz7057 Stanley RoadKearaburghCAMaximize dynamic e-commerce.
Shanahan, Robel and Beier378 Berta CrescentWest GerryKSSynthesize customized portals.
Kling - McLaughlin8346 Kertzmann SquareSouth JoesphIDReinvent cross-platform channels.
Jogi - McLaughlin83462 Shazam StreetNorth JoesphIDEliminate best-of-breed e-markets.
Jogi - McLaughlin83462 Shazam StreetNorth JoesphIDEliminate best-of-breed e-markets.
No records
'use client';

import { DataTable } from 'mantine-datatable';
import { companies } from '~/data';

export default function ResizingWithoutPersistenceExample() {
  return (
    <DataTable
      withTableBorder
      withColumnBorders
      records={companies}
      // 👇 no `storeColumnsKey` and no `useDataTableColumns()` hook needed
      columns={[
        { accessor: 'name', resizable: true },
        { accessor: 'streetAddress', resizable: true, ellipsis: true },
        { accessor: 'city', resizable: true, ellipsis: true },
        { accessor: 'state', resizable: true },
        { accessor: 'missionStatement', ellipsis: true },
      ]}
    />
  );
}

Combining resizing with other column features

Resizing can be combined with sorting, records selection, as well as column dragging and toggling. Widths, order and visibility are persisted under the same storeColumnsKey, and each can be reset independently. Notice how the example below avoids repetition by spreading a shared object into each column definition:

Connelly, Feest and Hodkiewicz7057 Stanley RoadKearaburghCA
Cummerata - Kuhlman6389 Dicki StreamSouth GateNH
Doyle, Hodkiewicz and O'Connell576 Joyce WaysTyraburghKS
Feest, Bogan and Herzog21716 Ratke DriveStromanportWY
Goldner, Rohan and Lehner632 Broadway AvenueNorth LouieWY
Goyette Inc8873 Mertz RapidDorthysideID
Jogi - McLaughlin83462 Shazam StreetNorth JoesphID
Jogi - McLaughlin83462 Shazam StreetNorth JoesphID
Kling - McLaughlin8346 Kertzmann SquareSouth JoesphID
Rau - O'Hara7508 Lansdowne RoadShieldsboroughMI
Runte Inc2996 Ronny MountMcAllenMA
Shanahan, Robel and Beier378 Berta CrescentWest GerryKS
Tillman - Jacobi57918 Gwendolyn CirclesSheridanportMI
No records
'use client';

import { DataTable, type DataTableSortStatus, useDataTableColumns } from 'mantine-datatable';
import { Button, Group, Stack, Switch } from '@mantine/core';
import { sortBy } from 'lodash';
import { useEffect, useState } from 'react';
import { type Company, companies } from '~/data';

export default function ResizingComplexExample() {
  const key = 'resize-complex-example';

  const [sortStatus, setSortStatus] = useState<DataTableSortStatus<Company>>({
    columnAccessor: 'name',
    direction: 'asc',
  });
  const [records, setRecords] = useState(sortBy(companies, 'name'));
  const [selectedRecords, setSelectedRecords] = useState<Company[]>([]);

  useEffect(() => {
    const data = sortBy(companies, sortStatus.columnAccessor);
    setRecords(sortStatus.direction === 'desc' ? data.reverse() : data);
  }, [sortStatus]);

  const [withTableBorder, setWithTableBorder] = useState(true);
  const [withColumnBorders, setWithColumnBorders] = useState(true);

  const props = {
    resizable: true,
    sortable: true,
    toggleable: true,
    draggable: true,
  };

  const { effectiveColumns, resetColumnsWidth, resetColumnsOrder, resetColumnsToggle } = useDataTableColumns<Company>({
    key,
    columns: [
      { accessor: 'name', ellipsis: true, ...props },
      { accessor: 'streetAddress', ellipsis: true, ...props },
      { accessor: 'city', ellipsis: true, ...props },
      { accessor: 'state', textAlign: 'right', ...props },
    ],
  });

  return (
    <Stack>
      <DataTable
        withTableBorder={withTableBorder}
        withColumnBorders={withColumnBorders}
        selectedRecords={selectedRecords}
        onSelectedRecordsChange={setSelectedRecords}
        storeColumnsKey={key}
        records={records}
        columns={effectiveColumns}
        sortStatus={sortStatus}
        onSortStatusChange={setSortStatus}
      />
      <Group grow justify="space-between">
        <Group justify="flex-start">
          <Switch
            checked={withTableBorder}
            onChange={(event) => setWithTableBorder(event.currentTarget.checked)}
            labelPosition="left"
            label="Table Border"
          />
          <Switch
            checked={withColumnBorders}
            onChange={(event) => setWithColumnBorders(event.currentTarget.checked)}
            labelPosition="left"
            label="Column Borders"
          />
        </Group>
        <Group justify="right">
          <Button size="compact-xs" onClick={resetColumnsWidth}>
            Reset Column Width
          </Button>
          <Button size="compact-xs" onClick={resetColumnsOrder}>
            Reset Column Order
          </Button>
          <Button size="compact-xs" onClick={resetColumnsToggle}>
            Reset Column Toggle
          </Button>
        </Group>
      </Group>
    </Stack>
  );
}

Head over to the next example to discover more features.

Mantine DataTable is used and trusted by