Theme

Examples › Dragging & Toggling

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 Dragging you’ll have to:
  • add a storeColumnsKey: 'your_key' property to the DataTable;
  • add a draggable: true property to each Dragging candidate column;
  • use the useDataTableColumns() hook to get the sorted columns.
import { DataTable, useDataTableColumns } from 'mantine-datatable';
import { companies, type Company } from '~/data';
import { Button, Group, Stack } from '@mantine/core';
export default function DraggingExample() {
const key = 'draggable-example';
const { effectiveColumns, resetColumnsOrder } = useDataTableColumns<Company>({
key,
columns: [
{ accessor: 'name', width: '40%', draggable: true },
{ accessor: 'streetAddress', width: '60%', draggable: true },
{ accessor: 'city', width: 160, ellipsis: true, draggable: true },
{ accessor: 'state' },
],
});
return (
<Stack>
<DataTable withBorder withColumnBorders storeColumnsKey={key} records={companies} columns={effectiveColumns} />
<Group position="right">
<Button onClick={resetColumnsOrder}>Reset Column Order</Button>
</Group>
</Stack>
);
}

Toggling

In the below example you can toggle the first 3 columns. The last column is not toggleable and will always be visible. The first column is toggled off by default. Click with right mouse button on the header to select the columns you want to toggle.
280 Rigoberto DivideTwin FallsMT
102 Konopelski GreensMissouri CityKY
230 Julie LakeHartfordKY
3387 Blick TurnpikeAttleboroWV
286 Leif LockColliervilleAL
09622 Marcel PlaceHighlandOR
8699 Upton FordsEast ProvidenceIN
191 O'Connell GreensSan RafaelMA
31622 Isobel FallOlatheAR
21751 Elisa VillageGrand PrairieWA
No records
In order to enable Toggling you’ll have to:
  • add a storeColumnsKey: 'your_key' property to the DataTable;
  • add a toggleable: true property to each Toggling candidate column;
  • use the useDataTableColumns() hook to get the sorted columns.
import { Button, Group, Stack, Text } from '@mantine/core';
import { IconBuildingCommunity, IconBuildingSkyscraper, IconMap, IconRoadSign } from '@tabler/icons-react';
import { DataTable, useDataTableColumns } from 'mantine-datatable';
import { companies } from '~/data';
export default function TogglingExample() {
const key = 'toggleable-example';
const { effectiveColumns, resetColumnsToggle } = useDataTableColumns({
key,
columns: [
{
accessor: 'name',
title: (
<Group spacing={4} mt={-1}>
<IconBuildingSkyscraper size={16} />
<Text inherit mt={1}>
Company
</Text>
</Group>
),
width: '40%',
toggleable: true,
defaultToggle: false,
},
{
accessor: 'streetAddress',
title: (
<Group spacing={4} mt={-1}>
<IconRoadSign size={16} />
<Text inherit mt={1}>
Street Address
</Text>
</Group>
),
width: '60%',
toggleable: true,
},
{
accessor: 'city',
title: (
<Group spacing={4} mt={-1}>
<IconBuildingCommunity size={16} />
<Text inherit mt={1}>
City
</Text>
</Group>
),
width: 160,
toggleable: true,
},
{
accessor: 'state',
width: 40,
textAlignment: 'right',
title: (
<Group position="right">
<IconMap size={16} />
</Group>
),
},
],
});
return (
<Stack>
<DataTable withBorder withColumnBorders storeColumnsKey={key} records={companies} columns={effectiveColumns} />
<Group position="right">
<Button onClick={resetColumnsToggle}>Reset Column Toggled</Button>
</Group>
</Stack>
);
}

Dragging & Toggling with context menu Reset

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
import { IconColumnRemove, IconColumns3 } from '@tabler/icons-react';
import { DataTable, useDataTableColumns } from 'mantine-datatable';
import { companies } from '~/data';
export default function DraggingTogglingResetExample() {
const key = 'toggleable-reset-example';
const { effectiveColumns, resetColumnsOrder, resetColumnsToggle } = useDataTableColumns({
key,
columns: [
{ accessor: 'name', width: '40%', toggleable: true, draggable: true },
{ accessor: 'streetAddress', width: '60%', toggleable: true, draggable: true },
{ accessor: 'city', width: 160, ellipsis: true, toggleable: true, draggable: true },
{ accessor: 'state' },
],
});
return (
<DataTable
withBorder
withColumnBorders
storeColumnsKey={key}
records={companies}
columns={effectiveColumns}
rowContextMenu={{
items: () => [
{
key: 'reset-toggled-columns',
icon: <IconColumnRemove size={16} />,
onClick: resetColumnsToggle,
},
{
key: 'reset-columns-order',
icon: <IconColumns3 size={16} />,
onClick: resetColumnsOrder,
},
],
}}
/>
);
}

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 { IconColumnRemove, IconColumns3 } from '@tabler/icons-react';
import sortBy from 'lodash/sortBy';
import { DataTable, DataTableSortStatus, useDataTableColumns } from 'mantine-datatable';
import { useEffect, useState } from 'react';
import { companies, type Company } from '~/data';
export default function DraggingTogglingComplexExample() {
const key = 'draggable-toggleable-complex-example';
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]);
const { effectiveColumns, resetColumnsOrder, resetColumnsToggle } = useDataTableColumns({
key,
columns: [
{ accessor: 'name', width: '40%', toggleable: true, draggable: true, sortable: true },
{ accessor: 'streetAddress', width: '60%', toggleable: true, draggable: true },
{ accessor: 'city', width: 160, ellipsis: true, toggleable: true, draggable: true },
{ accessor: 'state' },
],
});
return (
<DataTable
withBorder
withColumnBorders
storeColumnsKey={key}
records={records}
columns={effectiveColumns}
sortStatus={sortStatus}
onSortStatusChange={setSortStatus}
rowContextMenu={{
items: () => [
{
key: 'reset-toggled-columns',
icon: <IconColumnRemove size={16} />,
onClick: resetColumnsToggle,
},
{
key: 'reset-columns-order',
icon: <IconColumns3 size={16} />,
onClick: resetColumnsOrder,
},
],
}}
/>
);
}

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