search
HomeWeb Front-endJS Tutorialelement-ui implements import and export
element-ui implements import and exportApr 14, 2018 am 09:28 AM
element-uiaccomplishExport

This time I will bring you element-ui to implement import and export. What are the precautions for element-ui to implement import and export? The following is a practical case, let's take a look.

Preface

As we all know, ElementUI is a relatively complete UI library, but using it requires a bit of Vue foundation. Before starting the main text of this article, let's take a look at the installation method.

Install ElementUI module

cnpm install element-ui -S
Introduce

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-default/index.css'
in main.js Global installation

Vue.use(ElementUI)
When we finish the installation, remember to run it again,

cnpm run dev , and now you can use elementUI directly.

vue element-ui import and export function

1. Data display in the front-end backend management system generally uses tables, which involve import and export;

2. The import is to use the Upload component of element-ui;

<el-upload>//是否支持cookie信息发送
</el-upload>
3. The export is to use an object blob of file; get the data by calling the background interface, then use the data to instantiate the blob, and use the href attribute of the a tag to link to the blob object

 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. Paste the complete code of the entire small demo, which can be used directly in the background development (vue file)

<template>
<p>
 <el-table>
 <el-table-column>
 </el-table-column>
 <el-table-column>
  <template>{{ scope.row.date }}</template>
 </el-table-column>
 <el-table-column>
 </el-table-column>
 <el-table-column>
 </el-table-column>
 </el-table>
 </p>
<p>
 <el-button>切换第二、第三行的选中状态</el-button>
 <el-button>取消选择</el-button>
 <el-button>导入</el-button>
 <el-button>导出</el-button>
 </p>
 <!-- 导入 -->
 <el-dialog>
  <p>
  <el-upload>
  <!-- 是否支持发送cookie信息 -->
   <el-button>{{uploadTip}}</el-button>
   <p>只能上传excel文件</p>
  </el-upload>
  </p>
<p>
   <a>
   <i></i>下载模板</a>
  </p>
  
  <p>
  </p>
<p>
   <i></i>导入失败</p>
  <p>
   </p>
<h4 id="失败原因">失败原因</h4>
   <ul>
   <li>第{{error.rowIdx + 1}}行,错误:{{error.column}},{{error.value}},{{error.errorInfo}}</li>
   </ul>
  
  
 </el-dialog>
 <!-- 导出 -->

</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>
.hide-dialog{
 display:none;
}
</style>
5.js file, calling interface

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()
  })
 }
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

Angular verification function implementation steps

Detailed explanation of the steps to implement singleton mode in JS

The above is the detailed content of element-ui implements import and export. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Vue实现文件上传的完整指南(axios、element-ui)Vue实现文件上传的完整指南(axios、element-ui)Jun 09, 2023 pm 04:12 PM

Vue实现文件上传的完整指南(axios、element-ui)在现代Web应用程序中,文件上传已经成为一项基本的功能。无论是上传头像、图片、文档或者视频,我们都需要一个可靠的方法来将文件从用户的计算机上传到服务器中。本文将为您提供一份详细的指南,介绍如何使用Vue、axios和element-ui来实现文件上传。什么是axiosaxios是一个基于prom

如何使用Vue和Element-UI实现日历和日期选择功能如何使用Vue和Element-UI实现日历和日期选择功能Jul 22, 2023 pm 05:30 PM

如何使用Vue和Element-UI实现日历和日期选择功能简介:在前端开发中,日历和日期选择功能是非常常见的需求之一。Vue和Element-UI是一对非常强大的开发工具,结合它们可以轻松实现日历和日期选择功能。本文将介绍如何使用Vue和Element-UI来创建一个简单的日历和日期选择功能,并提供代码示例,帮助读者了解实现的具体步骤和方法。准备工作:在开始

如何使用Vue和Element-UI实现图片懒加载功能如何使用Vue和Element-UI实现图片懒加载功能Jul 22, 2023 pm 04:05 PM

如何使用Vue和Element-UI实现图片懒加载功能懒加载(Lazyloading)是一种通过延迟加载图片的技术,可以有效提升页面加载速度,节省带宽并改善用户体验。在Vue项目中,我们可以借助Element-UI和一些插件来实现图片懒加载功能。本文将介绍如何使用Vue和Element-UI来实现图片懒加载,并附上相应的代码示例。一、安装必要的依赖在开始之

如何使用Vue和Element-UI实现拖拽排序功能如何使用Vue和Element-UI实现拖拽排序功能Jul 22, 2023 pm 04:12 PM

如何使用Vue和Element-UI实现拖拽排序功能前言:在Web开发中,拖拽排序功能是一项常见且实用的功能。本文将介绍如何使用Vue和Element-UI来实现拖拽排序功能,通过代码示例演示实现过程。一、环境搭建安装Node.js在开始之前,需要安装Node.js。可以访问https://nodejs.org/下载并安装对应操作系统的版本。安装VueCL

如何使用Vue和Element-UI实现消息通知功能如何使用Vue和Element-UI实现消息通知功能Jul 21, 2023 pm 12:40 PM

如何使用Vue和Element-UI实现消息通知功能随着前端技术的不断发展,越来越多的网站和应用程序需要实现消息通知功能,以便及时向用户展示重要的信息。在Vue开发中,结合Element-UI框架可以快速实现这一功能。本文将详细介绍如何使用Vue和Element-UI来实现消息通知功能,并提供相关的代码示例。一、准备工作在使用Vue和Element-UI实现

如何使用Vue和Element-UI实现数据的筛选和搜索功能如何使用Vue和Element-UI实现数据的筛选和搜索功能Jul 21, 2023 pm 08:40 PM

如何使用Vue和Element-UI实现数据的筛选和搜索功能在现代Web开发中,数据的筛选和搜索功能是非常常见和重要的需求。Vue和Element-UI是目前非常流行的前端框架,它们提供了很多强大的工具和组件,可以帮助我们轻松实现数据的筛选和搜索功能。本文将介绍如何使用Vue和Element-UI来实现这些功能,并提供详细的代码示例。首先,我们需要准备一个用

如何使用Vue和Element-UI实现多级联动下拉框功能如何使用Vue和Element-UI实现多级联动下拉框功能Jul 20, 2023 pm 11:43 PM

如何使用Vue和Element-UI实现多级联动下拉框功能引言:在Web开发中,多级联动下拉框是一种常见的交互方式。通过选择一个下拉框的选项,可以动态改变后续下拉框的内容。本文将介绍如何使用Vue和Element-UI来实现这一功能,并提供代码示例。一、准备工作首先,我们需要确保已经安装好Vue和Element-UI。可以通过以下命令进行安装:npmins

如何使用Vue和Element-UI创建响应式网页界面如何使用Vue和Element-UI创建响应式网页界面Jul 20, 2023 pm 11:01 PM

如何使用Vue和Element-UI创建响应式网页界面在Web开发中,响应式设计是一种必不可少的技术。Vue.js和Element-UI是两个非常受欢迎的前端框架,它们都提供了丰富的工具和组件来构建现代化的响应式网页界面。本文将介绍如何使用Vue和Element-UI来创建响应式网页界面,并将通过代码示例来呈现具体的实现过程。首先,我们需要确保已经安装了Vu

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft