Material-table: Hidden columns do not get exported to CSV even if export: true is set

Created on 20 Nov 2019  路  7Comments  路  Source: mbrn/material-table

Describe the bug
Hidden columns do not get exported to CSV even if export: true is set.
This is how my column definition looks like:
{
title: "JSON",
field: "json",
export: true,
hidden: true
}

To Reproduce
Steps to reproduce the behavior:

  1. Create a column definition as written above (hidden: true, export: true)
  2. Export to CSV
  3. See that the column is missing from the export

Expected behavior
If a column has export: true defined it should be exported independently of its visibility.

Desktop (please complete the following information):

  • OS: Windows 10
  • Browser: Chrome
  • Version: 78.0.3904.108
enhancement

Most helpful comment

Hi @omega1990

You are right. It can export hidden columns if export==true for specified column. I will do this asap.

All 7 comments

This might help people having a similar issue. The workaround I did was:

function convertArrayOfObjectsToCSV(array, data) {
  let result;
  let keys = [];
  array.forEach(element => {
    keys.push(element.field);
  });

  const columnDelimiter = ",";
  const lineDelimiter = "\n";

  result = "";
  result += keys.join(columnDelimiter);
  result += lineDelimiter;

  data.forEach(item => {
    let ctr = 0;
    keys.forEach(key => {
      if (ctr > 0) result += columnDelimiter;

      result += item[key];

      ctr++;
    });
    result += lineDelimiter;
  });

  return result;
}

function downloadCSV(array, data) {
  const link = document.createElement("a");
  let csv = convertArrayOfObjectsToCSV(array, data);
  if (csv == null) return;

  const filename = "export.csv";

  if (!csv.match(/^data:text\/csv/i)) {
    csv = `data:text/csv;charset=utf-8,${csv}`;
  }

  link.setAttribute("href", encodeURI(csv));
  link.setAttribute("download", filename);
  link.click();
}

and then using:

  exportCsv: (columns, data) => {
            downloadCSV(columns, data);
          }

in material table options.

Hi @omega1990

You are right. It can export hidden columns if export==true for specified column. I will do this asap.

Hello, I like the current behavior, the user can decide which columns should be in the export and the table looks exactly the same (if all columns are allowed to export). If you change this, is it possible to get the current export behavior with a flag?

@mazzaker The logic would be that you hidden columns are not exported by default, however by setting export=true you would force it to be exported anyway.

@mbrn Thanks for replying so fast :)

Chiming in here, it'd be really nice to be able to pass an array of data to exportCsv column and allow material-table to do the work of outputting the CSV but with the provided data. Ideally, I'd like to lean on the ability of your library to do the CSV generation for me using its underlying code rather than duplicating code to generate CSVs.

Hello, any news regarding this?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

behrouz-s picture behrouz-s  路  3Comments

Mihier-Roy picture Mihier-Roy  路  3Comments

KKrisu picture KKrisu  路  3Comments

timrchavez picture timrchavez  路  3Comments

revskill10 picture revskill10  路  3Comments