Skip to content

Commit c93fcef

Browse files
committed
perf[UploadExcel-component]: tweaks
1 parent 9f8ac37 commit c93fcef

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

src/components/UploadExcel/index.vue

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<template>
22
<div>
3-
<input id="excel-upload-input" ref="excel-upload-input" type="file" accept=".xlsx, .xls" class="c-hide" @change="handkeFileChange">
3+
<input id="excel-upload-input" ref="excel-upload-input" type="file" accept=".xlsx, .xls" @change="handleClick">
44
<div id="drop" @drop="handleDrop" @dragover="handleDragover" @dragenter="handleDragover">
55
Drop excel file here or
6-
<el-button :loading="loading" style="margin-left:16px;" size="mini" type="primary" @click="handleUpload">browse</el-button>
6+
<el-button :loading="loading" style="margin-left:16px;" size="mini" type="primary" @click="handleUpload">Browse</el-button>
77
</div>
88
</div>
99
</template>
@@ -25,11 +25,12 @@ export default {
2525
generateDate({ header, results }) {
2626
this.excelData.header = header
2727
this.excelData.results = results
28-
this.$emit('on-selected-file', this.excelData)
28+
this.$emit('success', this.excelData)
2929
},
3030
handleDrop(e) {
3131
e.stopPropagation()
3232
e.preventDefault()
33+
if (this.loading) return
3334
const files = e.dataTransfer.files
3435
if (files.length !== 1) {
3536
this.$message.error('Only support uploading one file!')
@@ -48,17 +49,16 @@ export default {
4849
handleUpload() {
4950
document.getElementById('excel-upload-input').click()
5051
},
51-
handkeFileChange(e) {
52+
handleClick(e) {
5253
const files = e.target.files
5354
const itemFile = files[0] // only use files[0]
5455
if (!itemFile) return
55-
this.loading = true
5656
this.readerData(itemFile).then(() => {
5757
this.$refs['excel-upload-input'].value = null // fix can't select the same excel
58-
this.loading = false
5958
})
6059
},
6160
readerData(itemFile) {
61+
this.loading = true
6262
return new Promise((resolve, reject) => {
6363
const reader = new FileReader()
6464
reader.onload = e => {
@@ -70,6 +70,7 @@ export default {
7070
const header = this.get_header_row(worksheet)
7171
const results = XLSX.utils.sheet_to_json(worksheet)
7272
this.generateDate({ header, results })
73+
this.loading = false
7374
resolve()
7475
}
7576
reader.readAsArrayBuffer(itemFile)

src/views/excel/uploadExcel.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<div class="app-container">
3-
<upload-excel-component @on-selected-file='selected'></upload-excel-component>
3+
<upload-excel-component @success='handleSuccess'></upload-excel-component>
44
<el-table :data="tableData" border highlight-current-row style="width: 100%;margin-top:20px;">
55
<el-table-column v-for='item of tableHeader' :prop="item" :label="item" :key='item'>
66
</el-table-column>
@@ -21,9 +21,9 @@ export default {
2121
}
2222
},
2323
methods: {
24-
selected(data) {
25-
this.tableData = data.results
26-
this.tableHeader = data.header
24+
handleSuccess({ results, header }) {
25+
this.tableData = results
26+
this.tableHeader = header
2727
}
2828
}
2929
}

0 commit comments

Comments
 (0)