'How to set a filename for downloading csv file with react-table?
I am using react-table v7
, useExportData
and papaparse
to download a .csv from a table referencing to this codesandbox, so far it downloaded successfully but I can't find a way to name a file to download. My code is following
function getExportFileBlob({ columns, data, fileType, fileName }) {
if (fileType === "csv") {
// CSV example
const headerNames = columns.map((col) => col.exportValue);
const csvString = Papa.unparse({ fields: headerNames, data });
return new Blob([csvString], { type: "text/csv" });
}
Solution 1:[1]
Probably too late, but for those seeking for an answer, according to docs,
getExportFileName: Function({ fileType, all }) => string
is the function you need to override the default file name.
I believe you can access it from main useTable
where you are passing this plugin:
const {
....
exportData,
} = useTable(
{
getExportFileName: ({ all }) => {
return `${all ? 'exported-all' : 'current-view-only'}` // <--- there
},
},
useExportData
)
check it out, hope this helps!
Solution 2:[2]
You can use the getExportFileName function defined in the docs and overwrite the default all-data.fileType or data.fileType name. by returning a name for your exported data. Check out the following sandbox! Hope this helps.
`
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | YEVY |
Solution 2 | Eldrige |