這篇文章主要介紹了vue+element-ui怎么實現(xiàn)導(dǎo)入導(dǎo)出功能的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇vue+element-ui怎么實現(xiàn)導(dǎo)入導(dǎo)出功能文章都會有所收獲,下面我們一起來看看吧。
創(chuàng)新互聯(lián)公司是專業(yè)的成安網(wǎng)站建設(shè)公司,成安接單;提供網(wǎng)站設(shè)計、成都網(wǎng)站制作,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行成安網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
安裝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)我們安裝完記得重新運行,cnpm run dev
,現(xiàn)在就可以直接使用elementUI了。
vue + element-ui導(dǎo)入導(dǎo)出功能
1.前段后臺管理系統(tǒng)中數(shù)據(jù)展示一般都是用表格,表格會涉及到導(dǎo)入和導(dǎo)出;
2.導(dǎo)入是利用element-ui的Upload 上傳組件;
<el-upload class="upload-demo" :action="importUrl"//上傳的路徑 :name ="name"//上傳的文件字段名 :headers="importHeaders"//請求頭格式 :on-preview="handlePreview"//可以通過 file.response 拿到服務(wù)端返回數(shù)據(jù) :on-remove="handleRemove"//文件移除 :before-upload="beforeUpload"//上傳前配置 :on-error="uploadFail"//上傳錯誤 :on-success="uploadSuccess"//上傳成功 :file-list="fileList"//上傳的文件列表 :with-credentials="withCredentials">//是否支持cookie信息發(fā)送 </el-upload>
3.導(dǎo)出是利用file的一個對象blob;通過調(diào)用后臺接口拿到數(shù)據(jù),然后用數(shù)據(jù)來實例化blob,利用a標(biāo)簽的href屬性鏈接到blob對象
export const downloadTemplate = function (scheduleType) { axios.get('/rest/schedule/template', { params: { "scheduleType": scheduleType }, responseType: 'arraybuffer' }).then((response) => { //創(chuàng)建一個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的完整代碼,在后臺開發(fā)可以直接拿過去用(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">{{ 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 > <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"> <h5>失敗原因</h5> <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> <!-- 導(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',//后臺接口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) { //復(fù)選框選擇回填函數(shù),val返回一整行的數(shù)據(jù) this.multipleSelection = val; }, importData() { this.importFlag = 1 this.fileList = [] this.uploadTip = '點擊上傳' this.processing = false this.dialogImportVisible = true }, outportData() { scheduleApi.downloadTemplate() }, handlePreview(file) { //可以通過 file.response 拿到服務(wù)端返回數(shù)據(jù) }, handleRemove(file, fileList) { //文件移除 }, beforeUpload(file){ //上傳前配置 this.importHeaders.cityCode='上海'//可以配置請求頭 let excelfileExtend = ".xls,.xlsx"//設(shè)置文件格式 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('導(dǎo)入成功') this.doSearch() } }, //下載模板 download() { //調(diào)用后臺模板方法,和導(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)建一個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() }) }
關(guān)于“vue+element-ui怎么實現(xiàn)導(dǎo)入導(dǎo)出功能”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“vue+element-ui怎么實現(xiàn)導(dǎo)入導(dǎo)出功能”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
文章名稱:vue+element-ui怎么實現(xiàn)導(dǎo)入導(dǎo)出功能
本文URL:http://jinyejixie.com/article4/ijjsie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、網(wǎng)站策劃、全網(wǎng)營銷推廣、網(wǎng)站內(nèi)鏈、云服務(wù)器、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)