vue + element-ui實(shí)現(xiàn)簡(jiǎn)潔的導(dǎo)入導(dǎo)出功能
前言
眾所周知,ElementUI,是一個(gè)比較完善的UI庫(kù),但是使用它需要有一點(diǎn)vue的基礎(chǔ)。在開(kāi)始本文的正文之前,我們先來(lái)看看安裝的方法吧。
安裝ElementUI模塊
cnpm install element-ui -S
在main.js中引入
import ElementUI from 'element-ui' import 'element-ui/lib/theme-default/index.css'
全局安裝
Vue.use(ElementUI)
當(dāng)我們安裝完記得重新運(yùn)行,cnpm run dev
,現(xiàn)在就可以直接使用elementUI了。
vue + element-ui導(dǎo)入導(dǎo)出功能
1.前段后臺(tái)管理系統(tǒng)中數(shù)據(jù)展示一般都是用表格,表格會(huì)涉及到導(dǎo)入和導(dǎo)出;
2.導(dǎo)入是利用element-ui的Upload 上傳組件;
<el-upload class="upload-demo" :action="importUrl"http://上傳的路徑 :name ="name"http://上傳的文件字段名 :headers="importHeaders"http://請(qǐng)求頭格式 :on-preview="handlePreview"http://可以通過(guò) file.response 拿到服務(wù)端返回?cái)?shù)據(jù) :on-remove="handleRemove"http://文件移除 :before-upload="beforeUpload"http://上傳前配置 :on-error="uploadFail"http://上傳錯(cuò)誤 :on-success="uploadSuccess"http://上傳成功 :file-list="fileList"http://上傳的文件列表 :with-credentials="withCredentials">//是否支持cookie信息發(fā)送 </el-upload>
3.導(dǎo)出是利用file的一個(gè)對(duì)象blob;通過(guò)調(diào)用后臺(tái)接口拿到數(shù)據(jù),然后用數(shù)據(jù)來(lái)實(shí)例化blob,利用a標(biāo)簽的href屬性鏈接到blob對(duì)象
export const downloadTemplate = function (scheduleType) { axios.get('/rest/schedule/template', { params: { "scheduleType": scheduleType }, responseType: 'arraybuffer' }).then((response) => { //創(chuàng)建一個(gè)blob對(duì)象,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.貼上整個(gè)小demo的完整代碼,在后臺(tái)開(kāi)發(fā)可以直接拿過(guò)去用(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]])">切換第二、第三行的選中狀態(tài)</el-button> <el-button @click="toggleSelection()">取消選擇</el-button> <el-button type="primary" @click="importData">導(dǎo)入</el-button> <el-button type="primary" @click="outportData">導(dǎo)出</el-button> </div> <!-- 導(dǎo)入 --> <el-dialog title="導(dǎo)入" :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"> <!-- 是否支持發(fā)送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>導(dǎo)入失敗</div> <div class="failure-reason"> <h4>失敗原因</h4> <ul> <li v-for="(error,index) in errorResults" :key="index">第{{error.rowIdx + 1}}行,錯(cuò)誤:{{error.column}},{{error.value}},{{error.errorInfo}}</li> </ul> </div> </div> </el-dialog> <!-- 導(dǎo)出 --> </div> </template> <script> import * as scheduleApi from '@/api/schedule' export default { data() { return { tableData3: [ { date: "2016-05-03", name: "王小虎", address: "上海市普陀區(qū)金沙江路 1518 弄" }, { date: "2016-05-02", name: "王小虎", address: "上海市普陀區(qū)金沙江路 1518 弄" }, { date: "2016-05-04", name: "王小虎", address: "上海市普陀區(qū)金沙江路 1518 弄" }, { date: "2016-05-01", name: "王小虎", address: "上海市普陀區(qū)金沙江路 1518 弄" }, { date: "2016-05-08", name: "王小虎", address: "上海市普陀區(qū)金沙江路 1518 弄" }, { date: "2016-05-06", name: "王小虎", address: "上海市普陀區(qū)金沙江路 1518 弄" }, { date: "2016-05-07", name: "王小虎", address: "上海市普陀區(qū)金沙江路 1518 弄" } ], multipleSelection: [], importUrl:'www.baidu.com',//后臺(tái)接口config.admin_url+'rest/schedule/import/' importHeaders:{ enctype:'multipart/form-data', cityCode:'' }, name: 'import', fileList: [], withCredentials: true, processing: false, uploadTip:'點(diǎn)擊上傳', 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) { //復(fù)選框選擇回填函數(shù),val返回一整行的數(shù)據(jù) this.multipleSelection = val; }, importData() { this.importFlag = 1 this.fileList = [] this.uploadTip = '點(diǎn)擊上傳' this.processing = false this.dialogImportVisible = true }, outportData() { scheduleApi.downloadTemplate() }, handlePreview(file) { //可以通過(guò) file.response 拿到服務(wù)端返回?cái)?shù)據(jù) }, handleRemove(file, fileList) { //文件移除 }, beforeUpload(file){ //上傳前配置 this.importHeaders.cityCode='上海'//可以配置請(qǐng)求頭 let excelfileExtend = ".xls,.xlsx"http://設(shè)置文件格式 let fileExtend = file.name.substring(file.name.lastIndexOf('.')).toLowerCase(); if (excelfileExtend.indexOf(fileExtend) <= -1) { this.$message.error('文件格式錯(cuò)誤') return false } this.uploadTip = '正在處理中...' this.processing = true }, //上傳錯(cuò)誤 uploadFail(err, file, fileList) { this.uploadTip = '點(diǎn)擊上傳' this.processing = false this.$message.error(err) }, //上傳成功 uploadSuccess(response, file, fileList) { this.uploadTip = '點(diǎn)擊上傳' 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('導(dǎo)入成功') this.doSearch() } }, //下載模板 download() { //調(diào)用后臺(tái)模板方法,和導(dǎo)出類似 scheduleApi.downloadTemplate() }, } }; </script> <style scoped> .hide-dialog{ display:none; } </style>
5.js文件,調(diào)用接口
import axios from 'axios' // 下載模板 export const downloadTemplate = function (scheduleType) { axios.get('/rest/schedule/template', { params: { "scheduleType": scheduleType }, responseType: 'arraybuffer' }).then((response) => { //創(chuàng)建一個(gè)blob對(duì)象,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() }) }
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Vue.js中用v-bind綁定class的注意事項(xiàng)
關(guān)于數(shù)據(jù)綁定一個(gè)常見(jiàn)需求就是操作元素的class列表和它的內(nèi)聯(lián)樣式。因?yàn)樗鼈兌际菍傩裕覀兛梢杂?v-bind 處理它們,但是使用v-bind綁定class的時(shí)候我們要有一些注意事項(xiàng),下面這篇文章就給大家分享了下要注意的方面,希望能對(duì)大家有所幫助,下面來(lái)一起看看吧。2016-12-12vue3封裝數(shù)字滾動(dòng)組件的實(shí)現(xiàn)示例
本文主要介紹了vue3封裝數(shù)字滾動(dòng)組件的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08vue2.0嵌套路由實(shí)現(xiàn)豆瓣電影分頁(yè)功能(附demo)
這篇文章主要介紹了vue2.0嵌套路由實(shí)現(xiàn)豆瓣電影分頁(yè)功能(附demo),這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2017-03-03vue中的rules表單校驗(yàn)規(guī)則使用方法示例詳解 :rules=“rules“
這篇文章主要介紹了vue中的rules表單校驗(yàn)規(guī)則使用方法示例詳解 :rules=“rules“,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-11-11Vue使用mockjs問(wèn)題(返回?cái)?shù)據(jù)、get、post 請(qǐng)求)
這篇文章主要介紹了Vue使用mockjs問(wèn)題(返回?cái)?shù)據(jù)、get、post 請(qǐng)求),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2023-05-05從0到1構(gòu)建vueSSR項(xiàng)目之路由的構(gòu)建
這篇文章主要介紹了從0到1構(gòu)建vueSSR項(xiàng)目之路由的構(gòu)建,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03element plus中el-upload實(shí)現(xiàn)上傳多張圖片的示例代碼
最近寫項(xiàng)目的時(shí)候需要一次上傳多張圖片,本文主要介紹了element plus中el-upload實(shí)現(xiàn)上傳多張圖片的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01