Skip to content
This repository was archived by the owner on Mar 9, 2023. It is now read-only.

Commit b35c6a8

Browse files
authored
Merge pull request #26 from line/fix-#10
Can change the name of downloading file
2 parents 7e51ac3 + b44c27d commit b35c6a8

File tree

4 files changed

+29
-20
lines changed

4 files changed

+29
-20
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ Prop | Type | Default | Description
111111
`cols-label-text` | `String` | `'Columns'` | Text for the columns drag area
112112
`hide-settings-text` | `String` | `'Hide settings'` | Text for the "hide settings" button
113113
`show-settings-text` | `String` | `'Show settings'` | Text for the "show settings" button
114+
`filename` | `String` | (Determined by datetime) | Filename without extension for the downloading csv / tsv
114115

115116
#### Field format
116117

src/Pivot.vue

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,17 @@
5151

5252
<!-- Table -->
5353
<div class="col table-responsive pivottable">
54-
<pivot-table ref="pivottable" :data="data" :row-fields="internal.rowFields" :col-fields="internal.colFields" :reducer="reducer" :no-data-warning-text="noDataWarningText" :is-data-loading="isDataLoading" :style="{ height: tableHeight + 'px'}">
54+
<pivot-table
55+
ref="pivottable"
56+
:data="data"
57+
:row-fields="internal.rowFields"
58+
:col-fields="internal.colFields"
59+
:reducer="reducer"
60+
:no-data-warning-text="noDataWarningText"
61+
:filename="filename"
62+
:is-data-loading="isDataLoading"
63+
:style="{ height: tableHeight + 'px'}"
64+
>
5565
<!-- pass down scoped slots -->
5666
<template v-for="(slot, slotName) in $scopedSlots" :slot="slotName" slot-scope="{ value }">
5767
<slot :name="slotName" v-bind="{ value }"></slot>
@@ -130,6 +140,10 @@ export default {
130140
type: String,
131141
default: 'No data to display.'
132142
},
143+
filename: {
144+
type: String,
145+
default: ''
146+
},
133147
isDataLoading: {
134148
type: Boolean,
135149
default: false

src/PivotTable.vue

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484

8585
<script>
8686
import naturalSort from 'javascript-natural-sort'
87-
import { downloadTableWithTSV, downloadTableWithCSV } from './util'
87+
import { downloadTableWith } from './util'
8888
8989
export default {
9090
props: {
@@ -112,6 +112,10 @@ export default {
112112
type: String,
113113
default: 'Too many cells. Please reduce the num of rows / cols.'
114114
},
115+
filename: {
116+
type: String,
117+
default: ''
118+
},
115119
numOfCellsLimitation: {
116120
type: Number,
117121
default: 10000
@@ -309,16 +313,7 @@ export default {
309313
})
310314
},
311315
saveTableWithText (format) {
312-
switch (format) {
313-
case 'csv':
314-
downloadTableWithCSV(this.cols, this.colFields, this.rows, this.rowFields, this.rowHeaderSize, this.values)
315-
break
316-
case 'tsv':
317-
downloadTableWithTSV(this.cols, this.colFields, this.rows, this.rowFields, this.rowHeaderSize, this.values)
318-
break
319-
default:
320-
break
321-
}
316+
downloadTableWith(format, this.cols, this.colFields, this.rows, this.rowFields, this.rowHeaderSize, this.values, this.filename)
322317
}
323318
},
324319
watch: {

src/util.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
export function downloadTableWithTSV (cols, colFields, rows, rowFields, rowHeaderSize, values) {
2-
let filename = getFilenameByDate(new Date()) + '.tsv'
3-
formatAndDownloadWithText(cols, colFields, rows, rowFields, rowHeaderSize, values, '\t', filename)
4-
}
5-
6-
export function downloadTableWithCSV (cols, colFields, rows, rowFields, rowHeaderSize, values) {
7-
let filename = getFilenameByDate(new Date()) + '.csv'
8-
formatAndDownloadWithText(cols, colFields, rows, rowFields, rowHeaderSize, values, ',', filename)
1+
export function downloadTableWith (format, cols, colFields, rows, rowFields, rowHeaderSize, values, filename) {
2+
if (format !== 'tsv' && format !== 'csv') {
3+
throw Error('Invalid format on downloading, only "tsv" or "csv" can be used.')
4+
}
5+
const _filename = (filename || getFilenameByDate(new Date())) + '.' + format
6+
const delimiter = format === 'tsv' ? '\t' : ','
7+
formatAndDownloadWithText(cols, colFields, rows, rowFields, rowHeaderSize, values, delimiter, _filename)
98
}
109

1110
function toDoubleDigits (num) {

0 commit comments

Comments
 (0)