vue + element-ui实现简洁的导入导出功能
1.安装ElementUI模块
cnpm install element-ui -S
2.在main.js中引入
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-default/index.css'
3.全局安装
Vue.use(ElementUI)
4.当我们安装完记得重新运行,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"//可以通过 file.response 拿到服务端返回数据
: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) {
axios.get('/rest/schedule/template', {
params: {
"scheduleType": scheduleType
},
responseType: 'arraybuffer'
}).then((response) => {
//创建一个blob对象,file的一种
let blob = new Blob([response.data], { type: 'application/x-xls' })
let link = document.createElement('a')
link.href = window.URL.createObjectURL(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
style="width: 80%"
@selection-change="handleSelectionChange">
<el-table-column
type="selection"
width="55">
</el-table-column>
<el-table-column
label="日期"
width="120">
<template slot-scope="scope">{{ scope.row.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 style="margin-top: 20px">
<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">第{{error.rowIdx + 1}}行,错误:{{error.column}},{{error.value}},{{error.errorInfo}}</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 弄"
},
{
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.com',//后台接口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.$refs.multipleTable.toggleRowSelection(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) {
//可以通过 file.response 拿到服务端返回数据
},
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.$message.error('文件格式错误')
return false
}
this.uploadTip = '正在处理中...'
this.processing = true
},
//上传错误
uploadFail(err, file, fileList) {
this.uploadTip = '点击上传'
this.processing = false
this.$message.error(err)
},
//上传成功
uploadSuccess(response, file, fileList) {
this.uploadTip = '点击上传'
this.processing = false
if (response.status === -1) {
this.errorResults = response.data
if (this.errorResults) {
this.importFlag = 2
} else {
this.dialogImportVisible = false
this.$message.error(response.errorMsg)
}
} 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) {
axios.get('/rest/schedule/template', {
params: {
"scheduleType": scheduleType
},
responseType: 'arraybuffer'
}).then((response) => {
//创建一个blob对象,file的一种
let blob = new Blob([response.data], { type: 'application/x-xls' })
let link = document.createElement('a')
link.href = window.URL.createObjectURL(blob)
link.download = fileNames[scheduleType] + '_' + response.headers.datestr + '.xls'
link.click()
})
}
vue + element-ui实现简洁的导入导出功能的更多相关文章
- Vue+element ui table 导出到excel
需求: Vue+element UI table下的根据搜索条件导出当前所有数据 参考: https://blog.csdn.net/u010427666/article/details/792081 ...
- vue+element-ui的简洁导入导出功能
1.前段后台管理系统中数据展示一般都是用表格,表格会涉及到导入和导出;原生js导出excel2.导入是利用element-ui的Upload 上传组件; <el-upload class=&qu ...
- 分享一个自搭的框架,使用Spring boot+Vue+Element UI
废弃,新的:https://www.cnblogs.com/hackyo/p/10453243.html 特点:前后端分离,可遵循restful 框架:后端使用Spring boot,整合了aop.a ...
- 基于vue(element ui) + ssm + shiro 的权限框架
zhcc 基于vue(element ui) + ssm + shiro 的权限框架 引言 心声 现在的Java世界,各种资源很丰富,不得不说,从分布式,服务化,orm,再到前端控制,权限等等玲琅满目 ...
- Vue + Element UI 实现权限管理系统
Vue + Element UI 实现权限管理系统 前端篇(一):搭建开发环境 https://www.cnblogs.com/xifengxiaoma/p/9533018.html
- vue + element ui 实现实现动态渲染表格
前言:之前需要做一个页面,能够通过表名动态渲染出不同的表格,这里记录一下.转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9786326.html 网站地址:我的 ...
- vue + element ui 表格自定义表头,提供线上demo
前言:工作中用到 vue+element ui 的前端框架,需要使用自定义表头,需要使用 re.转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9710826.h ...
- vue+element ui 的上传文件使用组件
前言:工作中用到 vue+element ui 的前端框架,使用到上传文件,则想着封装为组件,达到复用,可扩展.转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9 ...
- vue+element ui 的表格列使用组件
前言:工作中用到 vue+element ui 的前端框架,有这个场景:很多表格的列有许多一样的,所以考虑将列封装为组件.转载请注明出处:https://www.cnblogs.com/yuxiaol ...
随机推荐
- java-day14
多线程程序访问共享数据会产生安全问题 解决线程安全问题 同步代码块 synchronized(锁对象){ 可能出现线程问题的代码 } 同步方法 修饰符 synchronized 返回值类型 方法名() ...
- 【ArcObject】 AxTocControl:实现图层可移动
设置axTocControl属性:EnableLayerDragDrop 为true即可
- 2019-8-31-dotnet-判断程序当前使用管理员运行降低权使用普通权限运行
title author date CreateTime categories dotnet 判断程序当前使用管理员运行降低权使用普通权限运行 lindexi 2019-08-31 16:55:58 ...
- element-ui表格点击一行展开
转载:https://www.cnblogs.com/xiaochongchong/p/8127282.html <template> <el-table :data="t ...
- 前端常用的库和实用技术之JavaScript 模块化
模块化概念 AMD是requirejs在推广过程中对模块化定义的规范化产出. 异步加载模块,依赖前置,提前执行 Define定义模块define(['require','foo'],function( ...
- 7.ICMP与ping
ping是基于ICMP(Internet Control Message Protocol)协议工作的 ICMP报文封装在IP包里,作为“侦察兵”,非常轻巧. ICMP报文的类型有很多,最常用的是 ...
- 【JZOJ6288】旋转子段
description analysis 可以先用前缀和把原串不调整的方案数先求出来 对于一种翻转,肯定是把\([i..a[i]]\)或\([a[i]..i]\)这段区间翻转 也可以看做是以\({i+ ...
- thinkphp 模板继承
模板继承是一项更加灵活的模板布局方式,模板继承不同于模板布局,甚至来说,应该在模板布局的上层.模板继承其实并不难理解,就好比类的继承一样,模板也可以定义一个基础模板(或者是布局),并且其中定义相关的区 ...
- poj 1742 Coins(二进制优化多重背包)
传送门 解题思路 多重背包,二进制优化.就是把每个物品拆分成一堆连续的\(2\)的幂加起来的形式,然后把最后剩下的也当成一个元素.直接类似\(0/1\)背包的跑就行了,时间复杂度\(O(nmlogc) ...
- HDU-6375-度度熊学队列-双端队列deque/list
度度熊正在学习双端队列,他对其翻转和合并产生了很大的兴趣. 初始时有 NN 个空的双端队列(编号为 11 到 NN ),你要支持度度熊的 QQ 次操作. ①11 uu ww valval 在编号为 u ...