fix button action export for csv

This commit is contained in:
Peter Clement 2022-03-15 12:03:16 +00:00
parent 65ea8e4e4d
commit 65e857609a
1 changed files with 26 additions and 1 deletions

View File

@ -241,6 +241,24 @@ const s3UploadHandler = async action => {
}
}
const convertToCsv = (headers, rows) => {
let csv = headers.map(key => `"${key}"`).join(",")
for (let row of rows) {
csv = `${csv}\n${headers
.map(header => {
let val = row[header]
val =
typeof val === "object"
? `"${JSON.stringify(val).replace(/"/g, "'")}"`
: `"${val}"`
return val.trim()
})
.join(",")}`
}
return csv
}
const exportDataHandler = async action => {
let selection = rowSelectionStore.actions.getSelection(
action.parameters.tableComponentId
@ -252,7 +270,14 @@ const exportDataHandler = async action => {
rows: selection.selectedRows,
})
download(JSON.stringify(data), `export.${action.parameters.type}`)
let dataToExport
const headers = Object.keys(data[0])
if (action.parameters.type === "csv") {
dataToExport = convertToCsv(headers, data)
} else {
dataToExport = JSON.stringify(data)
}
download(dataToExport, `export.${action.parameters.type}`)
} catch (error) {
notificationStore.actions.error("There was an error exporting the data")
}