PDF 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,
jspdf
and jspdf-autotable
are connected to some export buttons in the top toolbar to export table rows to a PDF 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 { jsPDF } from 'jspdf'; //or use your library of choice here10import autoTable from 'jspdf-autotable';11import { data, type Person } from './makeData';1213const columns: MRT_ColumnDef<Person>[] = [14{15accessorKey: 'id',16header: 'ID',17size: 40,18},19{20accessorKey: 'firstName',21header: 'First Name',22size: 120,23},24{25accessorKey: 'lastName',26header: 'Last Name',27size: 120,28},29{30accessorKey: 'company',31header: 'Company',32size: 300,33},34{35accessorKey: 'city',36header: 'City',37},38{39accessorKey: 'country',40header: 'Country',41size: 220,42},43];4445const Example = () => {46const handleExportRows = (rows: MRT_Row<Person>[]) => {47const doc = new jsPDF();48const tableData = rows.map((row) => Object.values(row.original));49const tableHeaders = columns.map((c) => c.header);5051autoTable(doc, {52head: [tableHeaders],53body: tableData,54});5556doc.save('mrt-pdf-example.pdf');57};5859const table = useMantineReactTable({60columns,61data,62enableRowSelection: true,63columnFilterDisplayMode: 'popover',64paginationDisplayMode: 'pages',65positionToolbarAlertBanner: 'bottom',66renderTopToolbarCustomActions: ({ table }) => (67<Box68sx={{69display: 'flex',70gap: '16px',71padding: '8px',72flexWrap: 'wrap',73}}74>75<Button76disabled={table.getPrePaginationRowModel().rows.length === 0}77//export all rows, including from the next page, (still respects filtering and sorting)78onClick={() =>79handleExportRows(table.getPrePaginationRowModel().rows)80}81leftIcon={<IconDownload />}82variant="filled"83>84Export All Rows85</Button>86<Button87disabled={table.getRowModel().rows.length === 0}88//export all rows as seen on the screen (respects pagination, sorting, filtering, etc.)89onClick={() => handleExportRows(table.getRowModel().rows)}90leftIcon={<IconDownload />}91variant="filled"92>93Export Page Rows94</Button>95<Button96disabled={97!table.getIsSomeRowsSelected() && !table.getIsAllRowsSelected()98}99//only export selected rows100onClick={() => handleExportRows(table.getSelectedRowModel().rows)}101leftIcon={<IconDownload />}102variant="filled"103>104Export Selected Rows105</Button>106</Box>107),108});109110return <MantineReactTable table={table} />;111};112113export default Example;
1import { MantineReactTable, useMantineReactTable } from 'mantine-react-table';2import { Box, Button } from '@mantine/core';3import { IconDownload } from '@tabler/icons-react';4import { jsPDF } from 'jspdf'; //or use your library of choice here5import autoTable from 'jspdf-autotable';6import { data } from './makeData';78const 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 Example = () => {41const handleExportRows = (rows) => {42const doc = new jsPDF();43const tableData = rows.map((row) => Object.values(row.original));44const tableHeaders = columns.map((c) => c.header);4546autoTable(doc, {47head: [tableHeaders],48body: tableData,49});5051doc.save('mrt-pdf-example.pdf');52};5354const table = useMantineReactTable({55columns,56data,57enableRowSelection: true,58columnFilterDisplayMode: 'popover',59paginationDisplayMode: 'pages',60positionToolbarAlertBanner: 'bottom',61renderTopToolbarCustomActions: ({ table }) => (62<Box63sx={{64display: 'flex',65gap: '16px',66padding: '8px',67flexWrap: 'wrap',68}}69>70<Button71disabled={table.getPrePaginationRowModel().rows.length === 0}72//export all rows, including from the next page, (still respects filtering and sorting)73onClick={() =>74handleExportRows(table.getPrePaginationRowModel().rows)75}76leftIcon={<IconDownload />}77variant="filled"78>79Export All Rows80</Button>81<Button82disabled={table.getRowModel().rows.length === 0}83//export all rows as seen on the screen (respects pagination, sorting, filtering, etc.)84onClick={() => handleExportRows(table.getRowModel().rows)}85leftIcon={<IconDownload />}86variant="filled"87>88Export Page Rows89</Button>90<Button91disabled={92!table.getIsSomeRowsSelected() && !table.getIsAllRowsSelected()93}94//only export selected rows95onClick={() => handleExportRows(table.getSelectedRowModel().rows)}96leftIcon={<IconDownload />}97variant="filled"98>99Export Selected Rows100</Button>101</Box>102),103});104105return <MantineReactTable table={table} />;106};107108export default Example;
View Extra Storybook Examples