Theme

Examples › Column resizing

Sipes Inc280 Rigoberto DivideTwin FallsMT
Runolfsdottir - Cummerata102 Konopelski GreensMissouri CityKY
Johnston LLC230 Julie LakeHartfordKY
Crist and Sons3387 Blick TurnpikeAttleboroWV
Schmidt and Sons286 Leif LockColliervilleAL
Nicolas Group09622 Marcel PlaceHighlandOR
Kub and Sons8699 Upton FordsEast ProvidenceIN
Jakubowski - Rolfson191 O'Connell GreensSan RafaelMA
Welch - Tremblay31622 Isobel FallOlatheAR
Mueller, Hodkiewicz and Beahan21751 Elisa VillageGrand PrairieWA
No records
In order to enable column resizing you’ll have to:
  • add a storeColumnsKey: your_key property to the DataTable (since the order of the columns is persisted in the local storage);
  • add a resizable: true property to each resizing candidate column;
  • use the useDataTableColumns() hook to get the effective columns.
import { Button, Group, Stack, Switch } from '@mantine/core';
import { DataTable, useDataTableColumns } from 'mantine-datatable';
import { useState } from 'react';
import { companies, type Company } from '~/data';
export default function ResizingExample() {
const key = 'resize-example';
const [withTableBorder, setWithTableBorder] = useState<boolean>(true);
const [withColumnBorders, setWithColumnBorders] = useState<boolean>(true);
const { effectiveColumns, resetColumnsWidth } = useDataTableColumns<Company>({
key,
columns: [
{ accessor: 'name', width: 100, resizable: true },
{ accessor: 'streetAddress', resizable: true },
{ accessor: 'city', resizable: true },
{ accessor: 'state' },
],
});
return (
<Stack>
<DataTable
withBorder={withTableBorder}
withColumnBorders={withColumnBorders}
storeColumnsKey={key}
records={companies}
columns={effectiveColumns}
/>
<Group grow position="apart">
<Group position="left">
<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 position="right">
<Button onClick={resetColumnsWidth}>Reset Column Width</Button>
</Group>
</Group>
</Stack>
);
}

Complex usage

Crist and Sons3387 Blick TurnpikeAttleboroWV
Jakubowski - Rolfson191 O'Connell GreensSan RafaelMA
Johnston LLC230 Julie LakeHartfordKY
Kub and Sons8699 Upton FordsEast ProvidenceIN
Mueller, Hodkiewicz and Beahan21751 Elisa VillageGrand PrairieWA
Nicolas Group09622 Marcel PlaceHighlandOR
Runolfsdottir - Cummerata102 Konopelski GreensMissouri CityKY
Schmidt and Sons286 Leif LockColliervilleAL
Sipes Inc280 Rigoberto DivideTwin FallsMT
Welch - Tremblay31622 Isobel FallOlatheAR
No records
import { Button, Group, Stack, Switch } from '@mantine/core';
import { sortBy } from 'lodash';
import { DataTable, DataTableSortStatus, useDataTableColumns } from 'mantine-datatable';
import { useEffect, useState } from 'react';
import { companies, type Company } from '~/data';
export default function ResizingComplexExample() {
const key = 'resize-complex-example';
const [withTableBorder, setWithTableBorder] = useState<boolean>(true);
const [withColumnBorders, setWithColumnBorders] = useState<boolean>(true);
const props = {
resizable: true,
sortable: true,
toggleable: true,
draggable: true,
};
const { effectiveColumns, resetColumnsWidth, resetColumnsOrder, resetColumnsToggle } = useDataTableColumns<Company>({
key,
columns: [
{ accessor: 'name', ...props },
{ accessor: 'streetAddress', ...props },
{ accessor: 'city', ...props },
{ accessor: 'state', ...props },
],
});
const [sortStatus, setSortStatus] = useState<DataTableSortStatus>({
columnAccessor: 'name',
direction: 'asc',
});
const [records, setRecords] = useState(sortBy(companies, 'name'));
useEffect(() => {
const data = sortBy(companies, sortStatus.columnAccessor) as Company[];
setRecords(sortStatus.direction === 'desc' ? data.reverse() : data);
}, [sortStatus]);
return (
<Stack>
<DataTable
withBorder={withTableBorder}
withColumnBorders={withColumnBorders}
storeColumnsKey={key}
records={records}
columns={effectiveColumns}
sortStatus={sortStatus}
onSortStatusChange={setSortStatus}
/>
<Group grow position="apart">
<Group position="left">
<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 position="right">
<Button size="xs" compact onClick={resetColumnsWidth}>
Reset Column Width
</Button>
<Button size="xs" compact onClick={resetColumnsOrder}>
Reset Column Order
</Button>
<Button size="xs" compact onClick={resetColumnsToggle}>
Reset Column Toggle
</Button>
</Group>
</Group>
</Stack>
);
}

Mantine DataTable is trusted by

MIT LicenseSponsor the author
Built by Ionut-Cristian Florescu and these awesome people.
Please sponsor the project if you find it useful.
GitHub StarsNPM Downloads