1
1
<template >
2
2
<div class =" csvdownload" >
3
- <button @click =" download" >{{ title }}</button >
3
+ <button @click =" download" >{{ title }}</button >
4
+ <optionModal v-on:downloadByModal =" emitDownload" v-on:sendOptions =" emitOptions" ></optionModal >
4
5
</div >
5
6
</template >
6
7
7
8
<script >
8
9
import Papa from ' papaparse'
9
10
import * as FS from ' file-saver'
10
11
12
+ import CsvOption from ' ./CsvOption.vue'
13
+
11
14
export default {
12
15
name: ' CsvDownload' ,
16
+ components: {
17
+ ' optionModal' : CsvOption
18
+ },
13
19
props: {
14
20
title: {
15
21
type: String ,
16
22
default: ' Download'
17
23
},
18
24
filename: {
19
25
type: String ,
20
- default: ' download.csv '
26
+ default: ' download'
21
27
},
22
- convOption: {
23
- type: Object
28
+ options: {
29
+ type: Object ,
30
+ default : () => {
31
+ return {
32
+ quotes: true ,
33
+ quoteChar: ' "' ,
34
+ delimiter: ' ,' ,
35
+ newline: ' \r\n ' ,
36
+ header: true ,
37
+ timestamp: false ,
38
+ comp: false
39
+ }
40
+ }
24
41
},
25
42
header: {
26
43
type: Object
@@ -35,9 +52,10 @@ export default {
35
52
}
36
53
},
37
54
methods: {
38
- convert () {
55
+ convert (options ) {
56
+ var dataCSV
39
57
if (typeof this .header === ' undefined' || this .header === ' ' ) {
40
- this . dataCSV = Papa .unparse (this .dataJson , this . convOption )
58
+ dataCSV = Papa .unparse (this .dataJson , options )
41
59
} else {
42
60
var d = {
43
61
fields: Object .values (this .header ),
@@ -51,17 +69,65 @@ export default {
51
69
}
52
70
d .data .push (a)
53
71
}
54
- this . dataCSV = Papa .unparse (d, this . convOption )
72
+ dataCSV = Papa .unparse (d, options )
55
73
}
74
+ return dataCSV
75
+ },
76
+ emitOptions (options ) {
77
+ this .options = options
78
+ },
79
+ emitDownload (options ) {
80
+ this ._download (options)
56
81
},
57
82
download () {
58
- this .convert ()
59
- if (typeof this .dataCSV === ' undefined' || this .dataCSV === ' ' ) {
83
+ this ._download (this .options )
84
+ },
85
+ _makeFilename (base , delimiter , timestamp ) {
86
+ var t = base
87
+ if (timestamp) {
88
+ const now = new Date ()
89
+ t += ' _' + now .getFullYear () +
90
+ (' 0' + (now .getMonth () + 1 )).slice (- 2 ) +
91
+ (' 0' + now .getDate ()).slice (- 2 ) +
92
+ (' 0' + now .getHours ()).slice (- 2 ) +
93
+ (' 0' + now .getMinutes ()).slice (- 2 )
94
+ }
95
+
96
+ switch (delimiter) {
97
+ case ' \t ' :
98
+ t += ' .tsv'
99
+ break
100
+ case ' ,' :
101
+ t += ' .csv'
102
+ break
103
+ default :
104
+ t += ' .txt'
105
+ break
106
+ }
107
+ return t
108
+ },
109
+ _download (options ) {
110
+ var filename
111
+ var opts
112
+
113
+ if (typeof options .filename === ' undefined' || options .filename === ' ' ) {
114
+ filename = this .filename
115
+ } else {
116
+ filename = options .filename
117
+ }
118
+ filename = this ._makeFilename (filename, options .delimiter , options .timestamp )
119
+ if (typeof options === ' undefined' || options === ' ' ) {
120
+ opts = this .options
121
+ } else {
122
+ opts = options
123
+ }
124
+ const dataCSV = this .convert (opts)
125
+ if (typeof dataCSV === ' undefined' || dataCSV === ' ' ) {
60
126
alert (' no data' )
61
127
return
62
128
}
63
- const blob = new Blob ([this . dataCSV ], {type: ' text/csv;charset=utf-8' })
64
- FS .saveAs (blob, this . filename )
129
+ const blob = new Blob ([dataCSV], {type: ' text/csv;charset=utf-8' })
130
+ FS .saveAs (blob, filename)
65
131
}
66
132
}
67
133
}
0 commit comments