CSV Export Example
Mantine React Table does not have a data exporting feature built-in. However, you can easily integrate your own exporting features.
In the example below,
export-to-csv
is connected to some export buttons in the top toolbar to export table rows to a CSV file that can be downloaded.ID | First Name | Last Name | Company | City | Country | |
---|---|---|---|---|---|---|
1 | Elenora | Wilkinson | Feest - Reilly | Hertaland | Qatar | |
2 | Berneice | Feil | Deckow, Leuschke and Jaskolski | Millcreek | Nepal | |
3 | Frieda | Baumbach | Heidenreich, Grady and Durgan | Volkmanside | Croatia | |
4 | Zachery | Brown | Cormier - Skiles | Faychester | Saint Pierre and Miquelon | |
5 | Kendra | Bins | Wehner - Wilderman | New Valentin | Senegal | |
6 | Lysanne | Fisher | Schmidt LLC | Malachitown | Costa Rica | |
7 | Garrick | Ryan | Ryan - Buckridge | East Pearl | Cocos (Keeling) Islands | |
8 | Hollis | Medhurst | Quitzon Group | West Sienna | Papua New Guinea | |
9 | Arlo | Buckridge | Konopelski - Spinka | Chino | Congo | |
10 | Rickie | Auer | Lehner - Walsh | Nyahfield | Sudan |
1import {2MantineReactTable,3useMantineReactTable,4type MRT_ColumnDef,5type MRT_Row,6} from 'mantine-react-table';7import { Box, Button } from '@mantine/core';8import { IconDownload } from '@tabler/icons-react';9import { mkConfig, generateCsv, download } from 'export-to-csv'; //or use your library of choice here10import { data, type Person } from './makeData';1112const columns: MRT_ColumnDef<Person>[] = [13{14accessorKey: 'id',15header: 'ID',16size: 40,17},18{19accessorKey: 'firstName',20header: 'First Name',21size: 120,22},23{24accessorKey: 'lastName',25header: 'Last Name',26size: 120,27},28{29accessorKey: 'company',30header: 'Company',31size: 300,32},33{34accessorKey: 'city',35header: 'City',36},37{38accessorKey: 'country',39header: 'Country',40size: 220,41},42];4344const csvConfig = mkConfig({45fieldSeparator: ',',46decimalSeparator: '.',47useKeysAsHeaders: true,48});4950const Example = () => {51const handleExportRows = (rows: MRT_Row<Person>[]) => {52const rowData = rows.map((row) => row.original);53const csv = generateCsv(csvConfig)(rowData);54download(csvConfig)(csv);55};5657const handleExportData = () => {58const csv = generateCsv(csvConfig)(data);59download(csvConfig)(csv);60};6162const table = useMantineReactTable({63columns,64data,65enableRowSelection: true,66columnFilterDisplayMode: 'popover',67paginationDisplayMode: 'pages',68positionToolbarAlertBanner: 'bottom',69renderTopToolbarCustomActions: ({ table }) => (70<Box71sx={{72display: 'flex',73gap: '16px',74padding: '8px',75flexWrap: 'wrap',76}}77>78<Button79color="lightblue"80//export all data that is currently in the table (ignore pagination, sorting, filtering, etc.)81onClick={handleExportData}82leftIcon={<IconDownload />}83variant="filled"84>85Export All Data86</Button>87<Button88disabled={table.getPrePaginationRowModel().rows.length === 0}89//export all rows, including from the next page, (still respects filtering and sorting)90onClick={() =>91handleExportRows(table.getPrePaginationRowModel().rows)92}93leftIcon={<IconDownload />}94variant="filled"95>96Export All Rows97</Button>98<Button99disabled={table.getRowModel().rows.length === 0}100//export all rows as seen on the screen (respects pagination, sorting, filtering, etc.)101onClick={() => handleExportRows(table.getRowModel().rows)}102leftIcon={<IconDownload />}103variant="filled"104>105Export Page Rows106</Button>107<Button108disabled={109!table.getIsSomeRowsSelected() && !table.getIsAllRowsSelected()110}111//only export selected rows112onClick={() => handleExportRows(table.getSelectedRowModel().rows)}113leftIcon={<IconDownload />}114variant="filled"115>116Export Selected Rows117</Button>118</Box>119),120});121122return <MantineReactTable table={table} />;123};124125export default Example;
1import { MantineReactTable, useMantineReactTable } from 'mantine-react-table';2import { Box, Button } from '@mantine/core';3import { IconDownload } from '@tabler/icons-react';4import { mkConfig, generateCsv, download } from 'export-to-csv'; //or use your library of choice here5import { data } from './makeData';67//defining columns outside of the component is fine, is stable8const columns = [9{10accessorKey: 'id',11header: 'ID',12size: 40,13},14{15accessorKey: 'firstName',16header: 'First Name',17size: 120,18},19{20accessorKey: 'lastName',21header: 'Last Name',22size: 120,23},24{25accessorKey: 'company',26header: 'Company',27size: 300,28},29{30accessorKey: 'city',31header: 'City',32},33{34accessorKey: 'country',35header: 'Country',36size: 220,37},38];3940const csvConfig = mkConfig({41fieldSeparator: ',',42decimalSeparator: '.',43useKeysAsHeaders: true,44});4546const Example = () => {47const handleExportRows = (rows) => {48const rowData = rows.map((row) => row.original);49const csv = generateCsv(csvConfig)(rowData);50download(csvConfig)(csv);51};5253const handleExportData = () => {54const csv = generateCsv(csvConfig)(data);55download(csvConfig)(csv);56};5758const table = useMantineReactTable({59columns,60data,61enableRowSelection: true,62columnFilterDisplayMode: 'popover',63paginationDisplayMode: 'pages',64positionToolbarAlertBanner: 'bottom',65renderTopToolbarCustomActions: ({ table }) => (66<Box67sx={{68display: 'flex',69gap: '16px',70padding: '8px',71flexWrap: 'wrap',72}}73>74<Button75color="lightblue"76//export all data that is currently in the table (ignore pagination, sorting, filtering, etc.)77onClick={handleExportData}78leftIcon={<IconDownload />}79variant="filled"80>81Export All Data82</Button>83<Button84disabled={table.getPrePaginationRowModel().rows.length === 0}85//export all rows, including from the next page, (still respects filtering and sorting)86onClick={() =>87handleExportRows(table.getPrePaginationRowModel().rows)88}89leftIcon={<IconDownload />}90variant="filled"91>92Export All Rows93</Button>94<Button95disabled={table.getRowModel().rows.length === 0}96//export all rows as seen on the screen (respects pagination, sorting, filtering, etc.)97onClick={() => handleExportRows(table.getRowModel().rows)}98leftIcon={<IconDownload />}99variant="filled"100>101Export Page Rows102</Button>103<Button104disabled={105!table.getIsSomeRowsSelected() && !table.getIsAllRowsSelected()106}107//only export selected rows108onClick={() => handleExportRows(table.getSelectedRowModel().rows)}109leftIcon={<IconDownload />}110variant="filled"111>112Export Selected Rows113</Button>114</Box>115),116});117118return <MantineReactTable table={table} />;119};120121export default Example;
1import {2MantineReactTable,3type MRT_ColumnDef,4type MRT_Row,5} from 'mantine-react-table';6import { Box, Button } from '@mantine/core';7import { IconDownload } from '@tabler/icons-react';8import { mkConfig, generateCsv, download } from 'export-to-csv'; //or use your library of choice here9import { data, type Person } from './makeData';1011//defining columns outside of the component is fine, is stable12const columns: MRT_ColumnDef<Person>[] = [13{14accessorKey: 'id',15header: 'ID',16size: 40,17},18{19accessorKey: 'firstName',20header: 'First Name',21size: 120,22},23{24accessorKey: 'lastName',25header: 'Last Name',26size: 120,27},28{29accessorKey: 'company',30header: 'Company',31size: 300,32},33{34accessorKey: 'city',35header: 'City',36},37{38accessorKey: 'country',39header: 'Country',40size: 220,41},42];4344const csvConfig = mkConfig({45fieldSeparator: ',',46decimalSeparator: '.',47useKeysAsHeaders: true,48});4950const Example = () => {51const handleExportRows = (rows: MRT_Row<Person>[]) => {52const rowData = rows.map((row) => row.original);53const csv = generateCsv(csvConfig)(rowData);54download(csvConfig)(csv);55};5657const handleExportData = () => {58const csv = generateCsv(csvConfig)(data);59download(csvConfig)(csv);60};6162return (63<MantineReactTable64columns={columns}65data={data}66enableRowSelection67columnFilterDisplayMode="popover"68paginationDisplayMode="pages"69positionToolbarAlertBanner="bottom"70renderTopToolbarCustomActions={({ table }) => (71<Box72sx={{73display: 'flex',74gap: '16px',75padding: '8px',76flexWrap: 'wrap',77}}78>79<Button80color="lightblue"81//export all data that is currently in the table (ignore pagination, sorting, filtering, etc.)82onClick={handleExportData}83leftIcon={<IconDownload />}84variant="filled"85>86Export All Data87</Button>88<Button89disabled={table.getPrePaginationRowModel().rows.length === 0}90//export all rows, including from the next page, (still respects filtering and sorting)91onClick={() =>92handleExportRows(table.getPrePaginationRowModel().rows)93}94leftIcon={<IconDownload />}95variant="filled"96>97Export All Rows98</Button>99<Button100disabled={table.getRowModel().rows.length === 0}101//export all rows as seen on the screen (respects pagination, sorting, filtering, etc.)102onClick={() => handleExportRows(table.getRowModel().rows)}103leftIcon={<IconDownload />}104variant="filled"105>106Export Page Rows107</Button>108<Button109disabled={110!table.getIsSomeRowsSelected() && !table.getIsAllRowsSelected()111}112//only export selected rows113onClick={() => handleExportRows(table.getSelectedRowModel().rows)}114leftIcon={<IconDownload />}115variant="filled"116>117Export Selected Rows118</Button>119</Box>120)}121/>122);123};124125export default Example;
View Extra Storybook Examples