I am using material-table and I want to remove the default CSV & PDF export option.
I would like to have only an excel option.
How can I change the menu ?
Thank you
To render a table with material-table, you have to supply the data (an array of objects) and the name of the columns to map with the data. The columns will specify which piece of data will go in which column.
There are two ways to use icons in material-table either import the material icons font via html OR import material icons and use the material-table icons prop.
The lookup is an object, and its definition you can find here in the first example https://mbrn.github.io/material-table/#/docz-examples-06-example-filtering. But if you tried to create that object outside and after that assign it to lookup you will get an error.
When you’re building data-heavy applications, you may need to enable users to export the data to Excel or PDF. Traditionally you would have to use some custom Excel library to do that job. With material-table, you simply pass an option that says exportButton : true, and you’re good to go.
With material-table, you simply pass an option that says exportButton : true, and you’re good to go. Now there will be an additional icon at the top of the table that enables users to download data both in .csv and .pdf format. Just a single line of code, and you’ve already supercharged your table.
Speaking of Angular Material team, they do not choose to support these kind of extra cases so it was left to community. The already published solutions and examples on the internet was either to export the native-table rendered in the page or to export the json array instead of the table itself.
material-table also enables you to sort your data very easily. All you have to do is pass another option, sorting : true. Remember, by default, material-table will try to sort your data lexicographically. If you want to sort with some other mechanism, you can modify that by overriding the columns property, like so:
For custom CSV and PDF, you should define options
options={{
exportButton: {
csv: true,
pdf: true,
}
}}
and for the handlers should be defined more options
options={{
exportButton: {
csv: true,
pdf: true,
},
exportCsv: (data, columns) => console.log(data, columns, '<== CSV'),
exportPdf: (data, columns) => console.log(data, columns, '<== PDF'),
}}
in the handler function for the CSV you can use filefy
package
import { CsvBuilder } from 'filefy';
and for PDF you can use jspdf
and jspdf-autotable
packages
import jsPDF from 'jspdf';
import 'jspdf-autotable';
also handler examples
exportCsv: (data, columns) => {
const columnTitles = columns
.map(columnDef => columnDef.title);
const csvData = data.map(rowData =>
columns.map(columnDef => rowData[columnDef.field]),
);
const builder = new CsvBuilder(`data.csv`)
.setColumns(columnTitles)
.addRows(csvData)
.exportFile();
return builder;
}
exportPdf: (data, columns) => {
const doc = new jsPDF();
const columnTitles = columns
.map(columnDef => columnDef.title);
const pdfData = data.map(rowData =>
columns.map(columnDef => rowData[columnDef.field]),
);
doc.autoTable({
head: [columnTitles],
body: pdfData,
});
doc.save(`data.pdf`);
}
Defining options
on the MT component like this will allow you to show/hide each option:
options={{
// ..other options
exportButton: {
csv: true,
pdf: false
}
}}
Also, you could use localization
settings to rename the label of each option like this:
localization={{
toolbar: {
exportCSVName: "Export some Excel format",
exportPDFName: "Export as pdf!!"
}
}}
It looks like the official docs are a bit outdated, so I found the answer to what you were looking for on these threads in GitHub:
Working sandbox here. Good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With