Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize default export option in material-table

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 ?

enter image description here

Thank you

like image 503
angus Avatar asked Nov 25 '20 13:11

angus


People also ask

How do I use Materialtable?

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.

How do I add an icon to a material-table?

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.

What is lookup in material-table?

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.

How do I export a material table to excel or PDF?

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.

How do I export data from material table to CSV?

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.

Is it possible to export the native table in Angular Material?

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.

How do I sort data in material-table?

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:


Video Answer


2 Answers

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`);
}
like image 188
Vahe Nikoghosyan Avatar answered Oct 17 '22 00:10

Vahe Nikoghosyan


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:

  • exportButton
  • localization

Working sandbox here. Good luck!

like image 20
NicoE Avatar answered Oct 16 '22 22:10

NicoE