I am using the custom function option for downloading a CSV usingMaterialTable. In the function I am modifying the data of only three columns.
When exportCsv is executed then the data array will contain the last changes which will results on a wrong output.
const downloadCsv = (data, fileName) => {
const finalFileName = `${fileName}.csv`;
const a = document.createElement("a");
a.href = URL.createObjectURL(new Blob([data], { type: "text/csv" }));
a.setAttribute("download", finalFileName);
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
export default function ReTable(props) {
const resultsData = useSelector((state) => state.results.data);
return (
<div>
<MaterialTable
columns={columns}
data={resultsData}
options={{
........
.......
.....
exportCsv: (columns, data) => {
const csvData = [...resultsData];
csvData.forEach(item => {
item.account = '="' + item.account + '"';
item.zip4 = '="' + item.zip4 + '"';
item.zip5 = '="' + item.zip5 + '"';
});
const dataRows = csvData.map(({ tableData, ...row }) => Object.values(row));
const csvContent = [header, ...dataRows].map(e => e.join(',')).join("\n");
downloadCsv(csvContent, props.name);
},
I don’t want to change the data so I have created a new csvData but apparently its it is effecting the data.
I am not sure what I am doing wrong ? I need to update the columns only ones.
Thank you