Data Export Example
Material React Table does not have a data exporting feature built in. However, you can easily integrate your own exporting feature, if desired.
In the example below, export-to-csv
is connected to some export buttons in the top toolbar. If you need to export in Excel or PDF format, there are a variety of NPM packages that can be used to export in these formats.
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 {2 MaterialReactTable,3 useMaterialReactTable,4 type MRT_ColumnDef,5 type MRT_Row,6} from 'material-react-table';7import { Box, Button } from '@mui/material';8import FileDownloadIcon from '@mui/icons-material/FileDownload';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 {14 accessorKey: 'id',15 header: 'ID',16 size: 40,17 },18 {19 accessorKey: 'firstName',20 header: 'First Name',21 size: 120,22 },23 {24 accessorKey: 'lastName',25 header: 'Last Name',26 size: 120,27 },28 {29 accessorKey: 'company',30 header: 'Company',31 size: 300,32 },33 {34 accessorKey: 'city',35 header: 'City',36 },37 {38 accessorKey: 'country',39 header: 'Country',40 size: 220,41 },42];4344const csvConfig = mkConfig({45 fieldSeparator: ',',46 decimalSeparator: '.',47 useKeysAsHeaders: true,48});4950const Example = () => {51 const handleExportRows = (rows: MRT_Row<Person>[]) => {52 const rowData = rows.map((row) => row.original);53 const csv = generateCsv(csvConfig)(rowData);54 download(csvConfig)(csv);55 };5657 const handleExportData = () => {58 const csv = generateCsv(csvConfig)(data);59 download(csvConfig)(csv);60 };6162 const table = useMaterialReactTable({63 columns,64 data,65 enableRowSelection: true,66 columnFilterDisplayMode: 'popover',67 paginationDisplayMode: 'pages',68 positionToolbarAlertBanner: 'bottom',69 renderTopToolbarCustomActions: ({ table }) => (70 <Box71 sx={{72 display: 'flex',73 gap: '16px',74 padding: '8px',75 flexWrap: 'wrap',76 }}77 >78 <Button79 //export all data that is currently in the table (ignore pagination, sorting, filtering, etc.)80 onClick={handleExportData}81 startIcon={<FileDownloadIcon />}82 >83 Export All Data84 </Button>85 <Button86 disabled={table.getPrePaginationRowModel().rows.length === 0}87 //export all rows, including from the next page, (still respects filtering and sorting)88 onClick={() =>89 handleExportRows(table.getPrePaginationRowModel().rows)90 }91 startIcon={<FileDownloadIcon />}92 >93 Export All Rows94 </Button>95 <Button96 disabled={table.getRowModel().rows.length === 0}97 //export all rows as seen on the screen (respects pagination, sorting, filtering, etc.)98 onClick={() => handleExportRows(table.getRowModel().rows)}99 startIcon={<FileDownloadIcon />}100 >101 Export Page Rows102 </Button>103 <Button104 disabled={105 !table.getIsSomeRowsSelected() && !table.getIsAllRowsSelected()106 }107 //only export selected rows108 onClick={() => handleExportRows(table.getSelectedRowModel().rows)}109 startIcon={<FileDownloadIcon />}110 >111 Export Selected Rows112 </Button>113 </Box>114 ),115 });116117 return <MaterialReactTable table={table} />;118};119120export default Example;121
View Extra Storybook Examples