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:
Expected behavior
If a column has export: true defined it should be exported independently of its visibility.
Desktop (please complete the following information):
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?
Most helpful comment
Hi @omega1990
You are right. It can export hidden columns if export==true for specified column. I will do this asap.