最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue + element-ui實現(xiàn)簡潔的導(dǎo)入導(dǎo)出功能

 更新時間:2017年12月22日 10:38:03   作者:火狼  
Element-UI是餓了么前端團隊推出的一款基于Vue.js 2.0 的桌面端UI框架,手機端有對應(yīng)框架是 Mint UI,下面這篇文章主要給大家介紹了關(guān)于利用vue + element-ui如何實現(xiàn)簡潔的導(dǎo)入導(dǎo)出功能的相關(guān)資料,需要的朋友可以參考下。

前言

眾所周知,ElementUI,是一個比較完善的UI庫,但是使用它需要有一點vue的基礎(chǔ)。在開始本文的正文之前,我們先來看看安裝的方法吧。

安裝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"http://上傳的路徑
  :name ="name"http://上傳的文件字段名
  :headers="importHeaders"http://請求頭格式
  :on-preview="handlePreview"http://可以通過 file.response 拿到服務(wù)端返回數(shù)據(jù)
  :on-remove="handleRemove"http://文件移除
  :before-upload="beforeUpload"http://上傳前配置
  :on-error="uploadFail"http://上傳錯誤
  :on-success="uploadSuccess"http://上傳成功
  :file-list="fileList"http://上傳的文件列表
  :with-credentials="withCredentials">//是否支持cookie信息發(fā)送
</el-upload>

3.導(dǎo)出是利用file的一個對象blob;通過調(diào)用后臺接口拿到數(shù)據(jù),然后用數(shù)據(jù)來實例化blob,利用a標簽的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
 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}}行,錯誤:{{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"http://設(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()
  })
 }

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • Vue之Watcher源碼解析(1)

    Vue之Watcher源碼解析(1)

    這篇文章主要為大家詳細介紹了Vue源碼之Watcher的基礎(chǔ)知識,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Vue SPA首屏加載緩慢問題解決方案

    Vue SPA首屏加載緩慢問題解決方案

    這篇文章主要介紹了Vue SPA首屏加載緩慢問題解決方案,首屏?xí)r間(First Contentful Paint),指的是瀏覽器從響應(yīng)用戶輸入網(wǎng)址地址,到首屏內(nèi)容渲染完成的時間,此時整個網(wǎng)頁不一定要全部渲染完成,但需要展示當(dāng)前視窗需要的內(nèi)容
    2023-03-03
  • 解決vant-UI庫修改樣式無效的問題

    解決vant-UI庫修改樣式無效的問題

    這篇文章主要介紹了解決vant-UI庫修改樣式無效的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • Vue3使用vant檢索組件van-search遇到的問題小結(jié)

    Vue3使用vant檢索組件van-search遇到的問題小結(jié)

    當(dāng)清空按鈕與檢索按鈕同時居右時,點擊clear清空按鈕事件時會同時觸發(fā)click-right-icon事件,這個時候容易觸發(fā)一系列問題,小編小編給大家分享Vue3使用vant檢索組件van-search遇到的問題小結(jié),感興趣的朋友一起看看吧
    2024-02-02
  • vue實現(xiàn)多條件篩選超簡潔代碼

    vue實現(xiàn)多條件篩選超簡潔代碼

    這篇文章主要給大家介紹了關(guān)于vue實現(xiàn)多條件篩選的相關(guān)資料,隨著數(shù)據(jù)的不斷增多,我們往往需要在表格中進行多條件的篩選,以便快速定位符合我們需求的數(shù)據(jù),需要的朋友可以參考下
    2023-09-09
  • 深入理解vue-loader如何使用

    深入理解vue-loader如何使用

    本篇文章主要介紹了vue-loader,vue-loader是webpack下loader插件 可以把.vue文件輸出成組件,有興趣的可以了解一下
    2017-06-06
  • 深入理解vue中slot與slot-scope的具體使用

    深入理解vue中slot與slot-scope的具體使用

    這篇文章主要介紹了深入理解vue中slot與slot-scope的具體使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • element-ui使用el-date-picker日期組件常見場景分析

    element-ui使用el-date-picker日期組件常見場景分析

    最近一直在使用 element-ui中的日期組件,所以想對日期組件常用的做一個簡單的總結(jié),對element-ui el-date-picker日期組件使用場景分析感興趣的朋友一起看看吧
    2024-05-05
  • Vue-cli打包后部署到子目錄下的路徑問題說明

    Vue-cli打包后部署到子目錄下的路徑問題說明

    這篇文章主要介紹了Vue-cli打包后部署到子目錄下的路徑問題說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • 基于VUE實現(xiàn)簡單的學(xué)生信息管理系統(tǒng)

    基于VUE實現(xiàn)簡單的學(xué)生信息管理系統(tǒng)

    這篇文章主要介紹了VUE實現(xiàn)一個簡單的學(xué)生信息管理系統(tǒng),本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01

最新評論

新河县| 双江| 大渡口区| 昆山市| 兴国县| 宽城| 惠水县| 崇明县| 芜湖市| 马公市| 大英县| 盘山县| 凯里市| 左云县| 石门县| 郧西县| 西畴县| 庐江县| 剑川县| 罗平县| 凯里市| 建湖县| 五常市| 巴青县| 怀远县| 曲松县| 吉水县| 克东县| 潞西市| 福鼎市| 马边| 桐梓县| 石渠县| 临朐县| 南木林县| 会宁县| 永丰县| 顺义区| 望都县| 马公市| 嘉义县|