Skip to content

Commit eb48219

Browse files
authored
Merge pull request #13 from ynishi/add_options
support options
2 parents 722cc1c + a81d3b6 commit eb48219

File tree

7 files changed

+604
-29
lines changed

7 files changed

+604
-29
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vuecsv",
3-
"version": "0.0.6",
3+
"version": "0.0.7",
44
"description": "CSV components for Vue.js",
55
"homepage": "https://github.com/ynishi/vuecsv",
66
"repository": {
@@ -21,6 +21,8 @@
2121
"prepare": "npm run build"
2222
},
2323
"dependencies": {
24+
"bootstrap": "4.0.0-beta.2",
25+
"bootstrap-vue": "^1.4.1",
2426
"file-saver": "^1.3.3",
2527
"papaparse": "^4.3.6",
2628
"testdouble": "^3.3.1",

src/components/CsvDownload.vue

Lines changed: 77 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,43 @@
11
<template>
22
<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>
45
</div>
56
</template>
67

78
<script>
89
import Papa from 'papaparse'
910
import * as FS from 'file-saver'
1011
12+
import CsvOption from './CsvOption.vue'
13+
1114
export default {
1215
name: 'CsvDownload',
16+
components: {
17+
'optionModal': CsvOption
18+
},
1319
props: {
1420
title: {
1521
type: String,
1622
default: 'Download'
1723
},
1824
filename: {
1925
type: String,
20-
default: 'download.csv'
26+
default: 'download'
2127
},
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+
}
2441
},
2542
header: {
2643
type: Object
@@ -35,9 +52,10 @@ export default {
3552
}
3653
},
3754
methods: {
38-
convert () {
55+
convert (options) {
56+
var dataCSV
3957
if (typeof this.header === 'undefined' || this.header === '') {
40-
this.dataCSV = Papa.unparse(this.dataJson, this.convOption)
58+
dataCSV = Papa.unparse(this.dataJson, options)
4159
} else {
4260
var d = {
4361
fields: Object.values(this.header),
@@ -51,17 +69,65 @@ export default {
5169
}
5270
d.data.push(a)
5371
}
54-
this.dataCSV = Papa.unparse(d, this.convOption)
72+
dataCSV = Papa.unparse(d, options)
5573
}
74+
return dataCSV
75+
},
76+
emitOptions (options) {
77+
this.options = options
78+
},
79+
emitDownload (options) {
80+
this._download(options)
5681
},
5782
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 === '') {
60126
alert('no data')
61127
return
62128
}
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)
65131
}
66132
}
67133
}

0 commit comments

Comments
 (0)