vue+element-ui实现简洁的导⼊导出功能
前⾔
众所周知,ElementUI,是⼀个⽐较完善的UI库,但是使⽤它需要有⼀点vue的基础。在开始本⽂的正⽂之前,我们先来看看安装的⽅法吧。
安装ElementUI模块
cnpm install element-ui -S
在main.js中引⼊
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-default/index.css'
全局安装
Vue.use(ElementUI)
当我们安装完记得重新运⾏,cnpm run dev ,现在就可以直接使⽤elementUI了。
vue + element-ui导⼊导出功能
1.前段后台管理系统中数据展⽰⼀般都是⽤表格,表格会涉及到导⼊和导出;
2.导⼊是利⽤element-ui的Upload 上传组件;
<el-upload class="upload-demo"
:action="importUrl"//上传的路径
:name ="name"//上传的⽂件字段名
:headers="importHeaders"//请求头格式
:on-preview="handlePreview"//可以通过 sponse 拿到服务端返回数据
:on-remove="handleRemove"//⽂件移除
:before-upload="beforeUpload"//上传前配置
:on-error="uploadFail"//上传错误
:
on-success="uploadSuccess"//上传成功
:file-list="fileList"//上传的⽂件列表
:with-credentials="withCredentials">//是否⽀持cookie信息发送
</el-upload>
3.导出是利⽤file的⼀个对象blob;通过调⽤后台接⼝拿到数据,然后⽤数据来实例化blob,利⽤a标签的href属性链接到blob对象 export const downloadTemplate = function (scheduleType) {
<('/rest/schedule/template', {
params: {
"scheduleType": scheduleType
},
responseType: 'arraybuffer'
}).then((response) => {
//创建⼀个blob对象,file的⼀种
let blob = new Blob([response.data], { type: 'application/x-xls' })
let link = ateElement('a')
link.href = ateObjectURL(blob)
//配置下载的⽂件名
link.download = fileNames[scheduleType] + '_' + response.headers.datestr + '.xls'
link.click()
})
}
4.贴上整个⼩demo的完整代码,在后台开发可以直接拿过去⽤(vue⽂件)
<template>
<div>
<el-table
ref="multipleTable"
:data="tableData3"
tooltip-effect="dark"
border
@selection-change="handleSelectionChange">
<el-table-column
type="selection"
width="55">
</el-table-column>
<el-table-column
label="⽇期"
width="120">
<template slot-scope="scope">{{ w.date }}</template>
</el-table-column>
<el-table-column
prop="name"
label="姓名"
width="120">
</el-table-column>
<el-table-column
prop="address"
label="地址"
show-overflow-tooltip>
</el-table-column>
</el-table>
<div >
<el-button @click="toggleSelection([tableData3[1], tableData3[2]])">切换第⼆、第三⾏的选中状态</el-button>
<el-button @click="toggleSelection()">取消选择</el-button>
<el-button type="primary" @click="importData">导⼊</el-button>
<el-button type="primary" @click="outportData">导出</el-button>
</div>
<!-- 导⼊ -->
<el-dialog title="导⼊" :visible.sync="dialogImportVisible" :modal-append-to-body="false" :close-on-click-modal="false" class="dialog-import">  <div :class="{'import-content': importFlag === 1, 'hide-dialog': importFlag !== 1}">
<el-upload class="upload-demo"
:action="importUrl"
:name ="name"
:headers="importHeaders"
:on-preview="handlePreview"
:on-remove="handleRemove"
:before-upload="beforeUpload"
:on-error="uploadFail"
:on-success="uploadSuccess"
:file-list="fileList"
:with-credentials="withCredentials">
<!-- 是否⽀持发送cookie信息 -->
<el-button size="small" type="primary" :disabled="processing">{{uploadTip}}</el-button>
<div slot="tip" class="el-upload__tip">只能上传excel⽂件</div>
</el-upload>
<div class="download-template">
<a class="btn-download" @click="download">
<i class="icon-download"></i>下载模板</a>
</div>
</div>
<div :class="{'import-failure': importFlag === 2, 'hide-dialog': importFlag !== 2}" >
<div class="failure-tips">
<i class="el-icon-warning"></i>导⼊失败</div>
<div class="failure-reason">
<h4>失败原因</h4>
<ul>
<li v-for="(error,index) in errorResults" :key="index">第{{wIdx + 1}}⾏,错误:{{lumn}},{{error.value}},{{Info}}</li>
</ul>
</div>
</div>
</el-dialog>
<!-- 导出 -->
</div>
</template>
<script>
import * as scheduleApi from '@/api/schedule'
export default {
data() {
return {
tableData3: [
{
date: "2016-05-03",
name: "王⼩虎",
address: "上海市普陀区⾦沙江路 1518 弄"
vue element admin},
{
date: "2016-05-02",
name: "王⼩虎",
address: "上海市普陀区⾦沙江路 1518 弄"
},
{
date: "2016-05-04",
name: "王⼩虎",
address: "上海市普陀区⾦沙江路 1518 弄"
},
{
date: "2016-05-01",
name: "王⼩虎",
address: "上海市普陀区⾦沙江路 1518 弄"
},
{
date: "2016-05-08",
name: "王⼩虎",
address: "上海市普陀区⾦沙江路 1518 弄"
},
{
date: "2016-05-06",
name: "王⼩虎",
address: "上海市普陀区⾦沙江路 1518 弄"
},
{
date: "2016-05-07",
name: "王⼩虎",
address: "上海市普陀区⾦沙江路 1518 弄"
}
],
multipleSelection: [],
importUrl:'www.baidu',//后台接⼝config.admin_url+'rest/schedule/import/'  importHeaders:{
enctype:'multipart/form-data',
cityCode:''
},
name: 'import',
fileList: [],
withCredentials: true,
processing: false,
uploadTip:'点击上传',
importFlag:1,
dialogImportVisible:false,
errorResults:[]
};
},
methods: {
toggleSelection(rows) {
if (rows) {
rows.forEach(row => {
this.$leRowSelection(row);
});
} else {
this.$refs.multipleTable.clearSelection();
}
},
handleSelectionChange(val) {
//复选框选择回填函数,val返回⼀整⾏的数据
this.multipleSelection = val;
},
importData() {
this.importFlag = 1
this.fileList = []
this.uploadTip = '点击上传'
this.processing = false
this.dialogImportVisible = true
},
outportData() {
scheduleApi.downloadTemplate()
},
handlePreview(file) {
//可以通过 sponse 拿到服务端返回数据
},
handleRemove(file, fileList) {
//⽂件移除
},
beforeUpload(file){
//上传前配置
this.importHeaders.cityCode='上海'//可以配置请求头
let excelfileExtend = ".xls,.xlsx"//设置⽂件格式
let fileExtend = file.name.substring(file.name.lastIndexOf('.')).toLowerCase();
if (excelfileExtend.indexOf(fileExtend) <= -1) {
this.$('⽂件格式错误')
return false
}
this.uploadTip = '正在处理中...'
this.processing = true
},
//上传错误
uploadFail(err, file, fileList) {
this.uploadTip = '点击上传'
this.processing = false
this.$(err)
},
/
/上传成功
uploadSuccess(response, file, fileList) {
this.uploadTip = '点击上传'
this.processing = false
if (response.status === -1) {
if (Results) {
this.importFlag = 2
} else {
this.dialogImportVisible = false
this.$(Msg)
}
} else {
this.importFlag = 3
this.dialogImportVisible = false
this.$message.info('导⼊成功')
this.doSearch()
}
},
//下载模板
download() {
//调⽤后台模板⽅法,和导出类似
scheduleApi.downloadTemplate()
},
}
};
</script>
<style scoped>
.hide-dialog{
display:none;
}
</style>
5.js⽂件,调⽤接⼝
import axios from 'axios'
// 下载模板
export const downloadTemplate = function (scheduleType) {
<('/rest/schedule/template', {
params: {
"scheduleType": scheduleType
},
responseType: 'arraybuffer'
}).then((response) => {
//创建⼀个blob对象,file的⼀种
let blob = new Blob([response.data], { type: 'application/x-xls' })
let link = ateElement('a')
link.href = ateObjectURL(blob)
link.download = fileNames[scheduleType] + '_' + response.headers.datestr + '.xls'
link.click()
})
}
总结
以上就是这篇⽂章的全部内容了,希望本⽂的内容对⼤家的学习或者⼯作具有⼀定的参考学习价值,如果有疑问⼤家可以留⾔交流,谢谢⼤家对的⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。