import '@mantine/core/styles.css';
import '@mantine/dates/styles.css';
import 'mantine-react-table/styles.css';
import {
MantineReactTable,
useMantineReactTable,
type MRT_ColumnDef,
type MRT_Row,
} from 'mantine-react-table';
import { Box, Button } from '@mantine/core';
import { IconDownload } from '@tabler/icons-react';
import { jsPDF } from 'jspdf';
import autoTable from 'jspdf-autotable';
import { data, type Person } from './makeData';
const columns: MRT_ColumnDef<Person>[] = [
{
accessorKey: 'id',
header: 'ID',
size: 40,
},
{
accessorKey: 'firstName',
header: 'First Name',
size: 120,
},
{
accessorKey: 'lastName',
header: 'Last Name',
size: 120,
},
{
accessorKey: 'company',
header: 'Company',
size: 300,
},
{
accessorKey: 'city',
header: 'City',
},
{
accessorKey: 'country',
header: 'Country',
size: 220,
},
];
const Example = () => {
const handleExportRows = (rows: MRT_Row<Person>[]) => {
const doc = new jsPDF();
const tableData = rows.map((row) => Object.values(row.original));
const tableHeaders = columns.map((c) => c.header);
autoTable(doc, {
head: [tableHeaders],
body: tableData,
});
doc.save('mrt-pdf-example.pdf');
};
const table = useMantineReactTable({
columns,
data,
enableRowSelection: true,
columnFilterDisplayMode: 'popover',
paginationDisplayMode: 'pages',
positionToolbarAlertBanner: 'bottom',
renderTopToolbarCustomActions: ({ table }) => (
<Box
style={{
display: 'flex',
gap: '16px',
padding: '8px',
flexWrap: 'wrap',
}}
>
<Button
disabled={table.getPrePaginationRowModel().rows.length === 0}
//export all rows, including from the next page, (still respects filtering and sorting)
onClick={() =>
handleExportRows(table.getPrePaginationRowModel().rows)
}
leftSection={<IconDownload />}
variant="filled"
>
Export All Rows
</Button>
<Button
disabled={table.getRowModel().rows.length === 0}
//export all rows as seen on the screen (respects pagination, sorting, filtering, etc.)
onClick={() => handleExportRows(table.getRowModel().rows)}
leftSection={<IconDownload />}
variant="filled"
>
Export Page Rows
</Button>
<Button
disabled={
!table.getIsSomeRowsSelected() && !table.getIsAllRowsSelected()
}
//only export selected rows
onClick={() => handleExportRows(table.getSelectedRowModel().rows)}
leftSection={<IconDownload />}
variant="filled"
>
Export Selected Rows
</Button>
</Box>
),
});
return <MantineReactTable table={table} />;
};
export default Example;