VUE+Element实现增删改查的示例源码

前言

&最近因为一些原因,没有更博客,昨天老师布置了一个作业,用vue实现增删改查功能,想想这也不难,就做一下试试吧。
因为自己写的样式没有别人做的好,因此我想用现成的UI框架,一直也没用过Element,就干脆趁机学一下吧。

实验步骤

首先引入一下element的css以及js

<!-- 引入样式 -->
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css" rel="external nofollow" rel="external nofollow" >
<!-- 引入组件库 -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>

然后引入需要用到的vue相关的js文件

<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

下面说一下HTML结构

<div id="app">
  <h1>职位的增删改查</h1>
  <div class="head">
    <el-row :gutter="20">
      <el-col :span="6">
        <el-input v-model="userInfo.name" placeholder="请输入你的公司名"></el-input>
      </el-col>
      <el-col :span="6">
        <el-input v-model="userInfo.position" placeholder="请输入你的职位"></el-input>
      </el-col>
      <el-col :span="6">
        <el-input v-model="userInfo.major" placeholder="请输入你的专业"></el-input>
      </el-col>
      <el-col :span="6">
        <el-input v-model="userInfo.number" placeholder="请输入数量"></el-input>
      </el-col>
    </el-row>
    <el-button type="primary" @click="addUser" class="add-btn" plain>添加信息</el-button>
  </div>
  <!-- 主体内容 -->
  <div class="body">
    <template>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="序号" width="180"><template slot-scope="scope"> {{scope.$index + 1 }} </template></el-table-column>
        <el-table-column prop="name" label="公司名" width="180"></el-table-column>
        <el-table-column prop="position" label="职位"></el-table-column>
        <el-table-column prop="major" label="专业"></el-table-column>
        <el-table-column prop="number" label="数量"></el-table-column>
        <el-table-column prop="birthday" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" @click="editUser(scope.row,scope.$index)" circle></el-button>
            <el-button type="danger" icon="el-icon-delete" @click="delUser(scope.$index)" circle></el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
  </div>
  <!-- 编辑框 -->
  <el-dialog title="编辑用户信息" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
    <div>
      <el-form ref="form" :model="editObj" label-width="80px">
        <el-form-item label="公司名"><el-input v-model="editObj.name"></el-input></el-form-item>
        <el-form-item label="职位"><el-input v-model="editObj.position"></el-input></el-form-item>
        <el-form-item label="专业"><el-input v-model="editObj.major"></el-input></el-form-item>
        <el-form-item label="数量"><el-input v-model="editObj.number"></el-input></el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </span>
  </el-dialog>
</div>

这一段是element的表单以及编辑等样式 ,其中添加了一些click操作 后面需要用到

加上基础的样式

 <style>
    #app{
      width:1024px;
      margin: 0 auto;
    }
    .add-btn{
      margin-top: 20px;
      width: 100%;
    }
    .body{
      margin-top:20px;
    }
  </style>

现在页面的基本样式就做好了,如下图所示:

下面开始写vue代码,对各个功能进行处理操作
了解过vuejs的应该知道这样的结构 data里面填写我们获取的数据 一些规则,定义一些变量 ,在methods进行我们的操作。

new Vue({
  el: '#app',
    data:{},
    methods:{}
})
data: function(){
        return{
          userInfo:{
            name:'',
            position: '',
            major: '',
            number: '',
          },
          tableData: [{
            name:'互联网+学院',
            position: '专职教师',
            major: '对外贸易',
            number: '2',
          },{
            name:'徐州重工',
            position: '工厂车研发部工程师',
            major: '精密机械制造',
            number: '12',
          },{
            name:'北京青码科技',
            position: '前端开发工程师',
            major: 'Vue、React',
            number: '4',
          }
          ],
          dialogVisible: false,
          editObj:{
            name:'',
            position: '',
            major: '',
            number: '',
          },
          userIndex:0,
        }
      },

接下来我们添加methods

  •     addUser() 是添加数据
  •     delUser()是删除数据
  •     editUser()是编辑数据
  •     handleClose()是是否弹出编辑框
  •     confirm()是确认信息并且传数据到表格中

在增加模块中,我做了信息判断,如果是信息是空就会弹出提示框,显示信息不能为空,
在删除模块中,点击可以删除一行信息
在修改模块中,会先将原本的信息拿到,然后再修改你需要修改的信息。

 methods:{
       //添加
        addUser(){
          if(!this.userInfo.name){
            this.$message({
              message: '请输入你的公司名!',

            });
            return;
          }
          if(!this.userInfo.position){
            this.$message({
              message: '请输入你的职位!',
              type: 'warning'
            });
            return;
          }
          if (!this.userInfo.major) {
            this.$message({
              message: '请输入你的专业!',
              type: 'warning'
            });
            return;
          }
          if (!this.userInfo.number) {
            this.$message({
              message: '请输入数量!',
              type: 'warning'
            });
            return;
          }
          this.tableData.push(this.userInfo);
          this.userInfo = {
            name:'',
            position: '',
            major: '',
            number: '',
          };
        },

        //删除
        delUser(idx){
          this.$confirm('确认删除此用户信息?')
            .then(_ => {
              this.tableData.splice(idx, 1);
            })
            .catch(_ => {});
        },
        //编辑
        editUser(item,idx){
          this.userIndex = idx;
          this.editObj = {
            name: item.name,
            position: item.position,
            major: item.major,
            number: item.number,
          };
          this.dialogVisible = true;
        },

        handleClose(){
          this.dialogVisible = false;
        },

        confirm(){
          this.dialogVisible = false;
          Vue.set(this.tableData, this.userIndex, this.editObj);
            }
          },
        })

总结:

    通过这次练习,让我知道了Element框架是怎么使用的,Element框架写代码做样式的确方便,以后有什么要求低的作业可以拿来使用,目前的我毕竟还是一个学生,我还是需要多锻炼写代码,手写样式的能力。

    最后: 附整个项目的源代码,本项目仅供学习交流。

源代码

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css" rel="external nofollow" rel="external nofollow" >
  <title>Vue增删改查</title>
  <style>
    #app{
      width:1024px;
      margin: 0 auto;
    }
    .add-btn{
      margin-top: 20px;
      width: 100%;
    }
    .body{
      margin-top:20px;
    }
  </style>

</head>
<body>
  <div id="app">
    <h1>职位的增删改查</h1>
    <div class="head">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input v-model="userInfo.name" placeholder="请输入你的公司名"></el-input>
        </el-col>
        <el-col :span="6">
          <el-input v-model="userInfo.position" placeholder="请输入你的职位"></el-input>
        </el-col>
        <el-col :span="6">
          <el-input v-model="userInfo.major" placeholder="请输入你的专业"></el-input>
        </el-col>
        <el-col :span="6">
          <el-input v-model="userInfo.number" placeholder="请输入数量"></el-input>
        </el-col>
      </el-row>
      <el-button type="primary" @click="addUser" class="add-btn" plain>添加信息</el-button>
    </div>
    <!-- 主体内容 -->
    <div class="body">
      <template>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column label="序号" width="180"><template slot-scope="scope"> {{scope.$index + 1 }} </template></el-table-column>
          <el-table-column prop="name" label="公司名" width="180"></el-table-column>
          <el-table-column prop="position" label="职位"></el-table-column>
          <el-table-column prop="major" label="专业"></el-table-column>
          <el-table-column prop="number" label="数量"></el-table-column>
          <el-table-column prop="birthday" label="操作">
            <template slot-scope="scope">
              <el-button type="primary" icon="el-icon-edit" @click="editUser(scope.row,scope.$index)" circle></el-button>
              <el-button type="danger" icon="el-icon-delete" @click="delUser(scope.$index)" circle></el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </div>
    <!-- 编辑框 -->
    <el-dialog title="编辑用户信息" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <div>
        <el-form ref="form" :model="editObj" label-width="80px">
          <el-form-item label="公司名"><el-input v-model="editObj.name"></el-input></el-form-item>
          <el-form-item label="职位"><el-input v-model="editObj.position"></el-input></el-form-item>
          <el-form-item label="专业"><el-input v-model="editObj.major"></el-input></el-form-item>
          <el-form-item label="数量"><el-input v-model="editObj.number"></el-input></el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <script src="https://unpkg.com/element-ui/lib/index.js"></script>

  <script>

    new Vue({
      el:'#app',
      data: function(){
        return{
          userInfo:{
            name:'',
            position: '',
            major: '',
            number: '',
          },
          tableData: [{
            name:'互联网+学院',
            position: '专职教师',
            major: '对外贸易',
            number: '2',
          },{
            name:'徐州重工',
            position: '工厂车研发部工程师',
            major: '精密机械制造',
            number: '12',
          },{
            name:'北京青码科技',
            position: '前端开发工程师',
            major: 'Vue、React',
            number: '4',
          }
          ],
          dialogVisible: false,
          editObj:{
            name:'',
            position: '',
            major: '',
            number: '',
          },
          userIndex:0,
        }
      },
      methods:{
        //添加
        addUser(){
          if(!this.userInfo.name){
            this.$message({
              message: '请输入你的公司名!',

            });
            return;
          }
          if(!this.userInfo.position){
            this.$message({
              message: '请输入你的职位!',
              type: 'warning'
            });
            return;
          }
          if (!this.userInfo.major) {
            this.$message({
              message: '请输入你的专业!',
              type: 'warning'
            });
            return;
          }
          if (!this.userInfo.number) {
            this.$message({
              message: '请输入数量!',
              type: 'warning'
            });
            return;
          }
          this.tableData.push(this.userInfo);
          this.userInfo = {
            name:'',
            position: '',
            major: '',
            number: '',
          };
        },

        //删除
        delUser(idx){
          this.$confirm('确认删除此用户信息?')
            .then(_ => {
              this.tableData.splice(idx, 1);
            })
            .catch(_ => {});
        },
        //编辑
        editUser(item,idx){
          this.userIndex = idx;
          this.editObj = {
            name: item.name,
            position: item.position,
            major: item.major,
            number: item.number,
          };
          this.dialogVisible = true;
        },

        handleClose(){
          this.dialogVisible = false;
        },

        confirm(){
          this.dialogVisible = false;
          Vue.set(this.tableData, this.userIndex, this.editObj);
            }
          },
        })
  </script>

</body>
</html>

以上就是VUE+Element实现增删改查的示例源码的详细内容,更多关于VUE+Element实现增删改查的资料请关注我们其它相关文章!

(0)

相关推荐

  • Spring boot + mybatis + Vue.js + ElementUI 实现数据的增删改查实例代码(一)

    环境搭建 spring boot的简介 以往我们开发时用到spring总是避免不了繁琐的配置,例如我们要配置一个数据库连接,可能需要以下几步: 1.编写jdbc.properties配置文件: 2.创建spring的配置文件,加入spring配置文件前缀.配置数据库连接信息以及sqlsessionFactory等等: 3.还要在web.xml文件中加入spring的监听. springboot的出现大大简化了项目的搭建过程(spring配置以及maven配置),让我们专注于应用功能的开发,而不是

  • 解决Vue+Element ui开发中碰到的IE问题

    IE9样式错乱,IE11无法正常加载v-loading等问题 引入了babel-polyfill插件,依然出现"polyfill-eventsource added missing EventSource to window"的奇怪问题(ie所有版本都有出现) 第一步:安装babel-ployfill (已安装请跳过此步骤) yarn add babel-ployfill 修改webpack打包配置文件:webpack.bash.conf.js // 引入babel-ployfill

  • Vue+ElementUI实现表单动态渲染、可视化配置的方法

    动态渲染就是有一个异步的数据,大概长这样: { "inline": true, "labelPosition": "right", "labelWidth": "", "size": "small", "statusIcon": true, "formItemList": [ { "type": "

  • Spring boot + mybatis + Vue.js + ElementUI 实现数据的增删改查实例代码(二)

    在上篇文章给大家介绍了Spring boot + mybatis + Vue.js + ElementUI 实现数据的增删改查实例代码(一),接下来我们添加分页相关的依赖,时间紧张,直接上代码了,贴上我的pom文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=

  • vue.js+Element实现表格里的增删改查

    新项目使用的是vue.js 后来发现饿了吗前端编写的一套框架Element (http://element.eleme.io/#/zh-CN)来配合vue.js进行样式填充 之前用过angularjs 用到后来 发现越来越难学 于是就决定用vue.js 下面就介绍一下vue.js应用在表格里的增删改查 首先引入一下element的js <script src="plugins/element-ui/index.js"></script> 然后引入需要用到的vue

  • 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

  • vue elementUI table表格数据 滚动懒加载的实现方法

    在项目中遇到了一个性能问题 vue+elementUI table表格展示数据,当数据很多的时候,不能一页显示完,同时一次请求数据量太大,会增加网页渲染的时间,影响体验, 这个时候常常有两种方法处理, 1.分页,如下 2.如果我不想分页,又想在一页显示全部数据呢?这个时候其实就可以用数据懒加载了 如下一开始表格只显示31行数据 当将滚动条拉到低的时候,就会再加载31条数据,如果剩下的数据不足31,那就加载剩下的 根据项目需求,这需要一页可以看到全部数据,所以我选择了第二中方式 那么第二种方式要怎

  • vue 2.0项目中如何引入element-ui详解

    前言 本文主要介绍了关于在vue 2.0项目中引入element-ui的相关内容,从新建vue项目到引入组件Element介绍的非常详细,下面话不多说了,来一起看看详细的介绍吧. 一.新建项目 1.查看 node和npm是不是已经安装好命令:node -v  npm -v (没有安装的先安装环境); 2.npm install -g cnpm --registry=https://registry.npm.taobao.org  (安装国内的淘宝镜像文件,后面的安装npm可以全部改为cnpm)

  • vue使用Element组件时v-for循环里的表单项验证方法

    标题描述看起来有些复杂,有vue,Element,又有表单验证,还有v-for循环?是不是有点乱?不过我相信开发中遇到过此问题的同学,一看就明白我说的意思了. 首先Element组件有一套完善的表单验证方法,官方文档写的也很清楚:Element表单验证API,正常按照官方文档添加rules规则,需要验证的表单项设置prop,然后提交表单时通过form的validate方法验证表单项就可以了. 然鹅问题来了,如果表单项里有通过v-for动态生成的表单项,如何设置验证呢?这个官方文档并没有明确的说法

  • 解决vue2.0 element-ui中el-upload的before-upload方法返回false时submit()不生效问题

    我要实现的功能是在上传文件之前校验是否表格中存在重复的数据,有的话,需要弹窗提示是否覆盖,确认之后继续上传,取消之后,就不再上传. 项目中用的element-ui是V1.4.3 <el-upload class="upload-demo" drag ref="fileUpload" :action="urls.fileUpload" :on-success="handleUploadSuccess" :on-error=

  • vue element项目引入icon图标的方法

    为了减少页面的加载速度,提高用户体验,对于一些图片决定使用图标代替,但是发现element-ui的图标少得可怜,完全满足不了我的要求,于是决定在element-ui的项目里引入第三方的图标库. 因为阿里巴巴海量的图标,所以决定引入阿里巴巴的图标库 阿里巴巴图标网站: http://www.iconfont.cn/ 下面具体介绍如何使用 1.注册一个阿里巴巴账号,搜索自己需要的图标,添加到购物车,然后点击添加到项目,如果没有项目就需要创建. 2.点击添加到项目后,选中第二个 3.点击更多操作里面的

随机推荐