Improve export to CSV functionality
- added`callback`option so we can get CSV data in case we do not want to download - added options: - `quoted` for cases when we have line breaks in content - `separator` for cases when we have commas in content
This commit is contained in:
parent
109465d3c5
commit
fb8479988c
2 changed files with 32 additions and 27 deletions
|
@ -714,8 +714,9 @@
|
||||||
let noHeader = false;
|
let noHeader = false;
|
||||||
if ('noHeader' in params) noHeader = params.noHeader;
|
if ('noHeader' in params) noHeader = params.noHeader;
|
||||||
|
|
||||||
const data = Csv(columns, datas, ',', noHeader);
|
const data = Csv(columns, datas, params, noHeader);
|
||||||
ExportCsv.download(params.filename, data);
|
if (params.callback) params.callback(data);
|
||||||
|
else ExportCsv.download(params.filename, data);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
created () {
|
created () {
|
||||||
|
|
|
@ -1,25 +1,39 @@
|
||||||
// https://github.com/Terminux/react-csv-downloader/blob/master/src/lib/csv.js
|
/*
|
||||||
|
inspired by https://www.npmjs.com/package/react-csv-downloader
|
||||||
|
now removed from Github
|
||||||
|
*/
|
||||||
|
|
||||||
const newLine = '\r\n';
|
const newLine = '\r\n';
|
||||||
|
const appendLine = (content, row, { separator, quoted }) => {
|
||||||
|
const line = row.map(data => {
|
||||||
|
if (!quoted) return data;
|
||||||
|
// quote data
|
||||||
|
data = typeof data === 'string' ? data.replace(/"/g, '"') : data;
|
||||||
|
return `"${data}"`;
|
||||||
|
});
|
||||||
|
content.push(line.join(separator));
|
||||||
|
};
|
||||||
|
|
||||||
export default function csv(columns, datas, separator = ',', noHeader = false) {
|
const defaults = {
|
||||||
|
separator: ',',
|
||||||
|
quoted: false
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function csv(columns, datas, options, noHeader = false) {
|
||||||
|
options = Object.assign({}, defaults, options);
|
||||||
let columnOrder;
|
let columnOrder;
|
||||||
const content = [];
|
const content = [];
|
||||||
const column = [];
|
const column = [];
|
||||||
|
|
||||||
if (columns) {
|
if (columns) {
|
||||||
columnOrder = columns.map(v => {
|
columnOrder = columns.map(v => {
|
||||||
if (typeof v === 'string') {
|
if (typeof v === 'string') return v;
|
||||||
return v;
|
|
||||||
}
|
|
||||||
if (!noHeader) {
|
if (!noHeader) {
|
||||||
column.push((typeof v.title !== 'undefined') ? v.title : v.key);
|
column.push(typeof v.title !== 'undefined' ? v.title : v.key);
|
||||||
}
|
}
|
||||||
return v.key;
|
return v.key;
|
||||||
});
|
});
|
||||||
if (column.length > 0) {
|
if (column.length > 0) appendLine(content, column, options);
|
||||||
content.push(column.join(separator));
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
columnOrder = [];
|
columnOrder = [];
|
||||||
datas.forEach(v => {
|
datas.forEach(v => {
|
||||||
|
@ -29,27 +43,17 @@ export default function csv(columns, datas, separator = ',', noHeader = false) {
|
||||||
});
|
});
|
||||||
if (columnOrder.length > 0) {
|
if (columnOrder.length > 0) {
|
||||||
columnOrder = columnOrder.filter((value, index, self) => self.indexOf(value) === index);
|
columnOrder = columnOrder.filter((value, index, self) => self.indexOf(value) === index);
|
||||||
|
if (!noHeader) appendLine(content, columnOrder, options);
|
||||||
if (!noHeader) {
|
|
||||||
content.push(columnOrder.join(separator));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(datas)) {
|
if (Array.isArray(datas)) {
|
||||||
datas.map(v => {
|
datas.forEach(row => {
|
||||||
if (Array.isArray(v)) {
|
if (!Array.isArray(row)) {
|
||||||
return v;
|
row = columnOrder.map(k => (typeof row[k] !== 'undefined' ? row[k] : ''));
|
||||||
}
|
}
|
||||||
return columnOrder.map(k => {
|
appendLine(content, row, options);
|
||||||
if (typeof v[k] !== 'undefined') {
|
|
||||||
return v[k];
|
|
||||||
}
|
|
||||||
return '';
|
|
||||||
});
|
|
||||||
}).forEach(v => {
|
|
||||||
content.push(v.join(separator));
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return content.join(newLine);
|
return content.join(newLine);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue