ホームページ  >  記事  >  ウェブフロントエンド  >  vue + element-ui でインポートとエクスポートを実装する方法

vue + element-ui でインポートとエクスポートを実装する方法

亚连
亚连オリジナル
2018-06-19 10:11:593489ブラウズ

Element-UI は、Ele.me フロントエンド チームによって開始された Vue.js 2.0 に基づくデスクトップ UI フレームワークです。携帯電話用の対応するフレームワークは、主に 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. ui アップロード コンポーネント;

<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. エクスポートは、ファイルを使用してオブジェクト BLOB です。バックグラウンド インターフェイスを呼び出してデータを取得し、そのデータを使用して BLOB をインスタンス化し、a タグの href 属性を使用して、 BLOB オブジェクト

 export const downloadTemplate = function (scheduleType) {
  axios.get(&#39;/rest/schedule/template&#39;, {
   params: {
    "scheduleType": scheduleType
   },
   responseType: &#39;arraybuffer&#39;
  }).then((response) => {
   //创建一个blob对象,file的一种
   let blob = new Blob([response.data], { type: &#39;application/x-xls&#39; })
   let link = document.createElement(&#39;a&#39;)
   link.href = window.URL.createObjectURL(blob)
   //配置下载的文件名
   link.download = fileNames[scheduleType] + &#39;_&#39; + response.headers.datestr + &#39;.xls&#39;
   link.click()
  })
 }

4. ポスト小さなデモ全体の完全なコードは、バックグラウンド開発 (vue ファイル)

<template>
<p>
 <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>
 <p 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>
 </p>
 <!-- 导入 -->
 <el-dialog title="导入" :visible.sync="dialogImportVisible" :modal-append-to-body="false" :close-on-click-modal="false" class="dialog-import">
  <p :class="{&#39;import-content&#39;: importFlag === 1, &#39;hide-dialog&#39;: 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>
   <p slot="tip" class="el-upload__tip">只能上传excel文件</p>
  </el-upload>
  <p class="download-template">
   <a class="btn-download" @click="download">
   <i class="icon-download"></i>下载模板</a>
  </p>
  </p>
  <p :class="{&#39;import-failure&#39;: importFlag === 2, &#39;hide-dialog&#39;: importFlag !== 2}" >
  <p class="failure-tips">
   <i class="el-icon-warning"></i>导入失败</p>
  <p 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>
  </p>
  </p>
 </el-dialog>

 <!-- 导出 -->
</p>
</template>
<script>
import * as scheduleApi from &#39;@/api/schedule&#39;
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:&#39;www.baidu.com&#39;,//后台接口config.admin_url+&#39;rest/schedule/import/&#39;
  importHeaders:{
  enctype:&#39;multipart/form-data&#39;,
  cityCode:&#39;&#39;
  },
  name: &#39;import&#39;,
  fileList: [],
  withCredentials: true,
  processing: false,
  uploadTip:&#39;点击上传&#39;,
  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 = &#39;点击上传&#39;
  this.processing = false
  this.dialogImportVisible = true
 },
 outportData() {
  scheduleApi.downloadTemplate()
 },
 handlePreview(file) {
  //可以通过 file.response 拿到服务端返回数据
 },
 handleRemove(file, fileList) {
  //文件移除
 },
 beforeUpload(file){
  //上传前配置
  this.importHeaders.cityCode=&#39;上海&#39;//可以配置请求头
  let excelfileExtend = ".xls,.xlsx"//设置文件格式
  let fileExtend = file.name.substring(file.name.lastIndexOf(&#39;.&#39;)).toLowerCase();
  if (excelfileExtend.indexOf(fileExtend) <= -1) {
   this.$message.error(&#39;文件格式错误&#39;)
   return false
  }
  this.uploadTip = &#39;正在处理中...&#39;
  this.processing = true
 },
 //上传错误
 uploadFail(err, file, fileList) {
  this.uploadTip = &#39;点击上传&#39;
  this.processing = false
  this.$message.error(err)
 },
 //上传成功
 uploadSuccess(response, file, fileList) {
  this.uploadTip = &#39;点击上传&#39;
  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(&#39;导入成功&#39;)
  this.doSearch()
  }
 },
 //下载模板
 download() {
  //调用后台模板方法,和导出类似
  scheduleApi.downloadTemplate()
 },
 }
};
</script>
<style scoped>
.hide-dialog{
 display:none;
}
</style>

5.js ファイルで直接使用でき、インターフェース

import axios from &#39;axios&#39;
// 下载模板
 export const downloadTemplate = function (scheduleType) {
  axios.get(&#39;/rest/schedule/template&#39;, {
   params: {
    "scheduleType": scheduleType
   },
   responseType: &#39;arraybuffer&#39;
  }).then((response) => {
   //创建一个blob对象,file的一种
   let blob = new Blob([response.data], { type: &#39;application/x-xls&#39; })
   let link = document.createElement(&#39;a&#39;)
   link.href = window.URL.createObjectURL(blob)
   link.download = fileNames[scheduleType] + &#39;_&#39; + response.headers.datestr + &#39;.xls&#39;
   link.click()
  })
 }

を呼び出します。上記は私が皆さんのためにコンパイルしたものです。今後皆さんのお役に立てば幸いです。

関連記事:

vue + elementでテーブルページングを実装する方法

WeChatアプレットに記録と再生の記録機能を実装する方法

Nuxt.jsフレームワーク(詳細なチュートリアル)

での方法JS+CSS でローリングデジタル時計を実装する方法

JS で画像の中央浮動効果を実装する方法

Angular 4.0 でlaydate.js 日付プラグインを使用する方法

実装方法jQueryのオンラインカスタマーサービス機能

以上がvue + element-ui でインポートとエクスポートを実装する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。