export-csv.js
1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
function getDownloadUrl(content) {
var BOM = '\uFEFF';
if (window.Blob && window.URL && window.URL.createObjectURL) {
var csvData = new Blob([BOM + content], { type: 'text/csv' });
return URL.createObjectURL(csvData);
} else {
return 'data:attachment/csv;charset=utf-8,' + BOM + encodeURIComponent(content);
}
}
function getContent(columns, tableData) {
var separator = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ',';
var newLine = '\r\n';
var result = [],
csvRows = [],
_tableData = [];
if (Array.isArray(columns) && columns.length > 0) {
csvRows = columns.map(function (x) {
return x.title;
});
result.push(csvRows.join(separator));
}
if (Array.isArray(tableData) && tableData.length > 0) {
tableData.forEach(function (row) {
csvRows = columns.map(function (col) {
if (row[col.field]) {
return row[col.field];
}
return '';
});
result.push(csvRows.join(separator));
});
}
return result.join(newLine);
}
var csv = {
download: function download(fileName, columns, tableData) {
var content = getContent(columns, tableData);
if (content && content.length > 0) {
var link = document.createElement('a');
link.download = fileName;
link.href = getDownloadUrl(content);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
} else {
console.error('vue-easytable::no export data');
}
}
};
exports.default = csv;