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


Posted in Vue.js onNovember 23, 2020

前言

&最近因为一些原因,没有更博客,昨天老师布置了一个作业,用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+Element实现增删改查的示例源码

下面开始写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实现增删改查的资料请关注三水点靠木其它相关文章!

Vue.js 相关文章推荐
vue 页面跳转的实现方式
Jan 12 Vue.js
Vue如何实现变量表达式选择器
Feb 18 Vue.js
Vue包大小优化的实现(从1.72M到94K)
Feb 18 Vue.js
浅谈vue2的$refs在vue3组合式API中的替代方法
Apr 18 Vue.js
如何理解Vue简单状态管理之store模式
May 15 Vue.js
vue-element-admin项目导入和导出的实现
May 21 Vue.js
vue中利用mqtt服务端实现即时通讯的步骤记录
Jul 01 Vue.js
vue3中provide && inject的使用
Jul 01 Vue.js
深入讲解Vue中父子组件通信与事件触发
Mar 22 Vue.js
vue如何使用模拟的json数据查看效果
Mar 31 Vue.js
VUE之图片Base64编码使用ElementUI组件上传
Apr 09 Vue.js
vue elementUI表格控制对应列
Apr 13 Vue.js
Vue实现购物小球抛物线的方法实例
Nov 22 #Vue.js
vue自定义插件封装,实现简易的elementUi的Message和MessageBox的示例
Nov 20 #Vue.js
详解vue 组件注册
Nov 20 #Vue.js
vue-drawer-layout实现手势滑出菜单栏
Nov 19 #Vue.js
Vue 打包的静态文件不能直接运行的原因及解决办法
Nov 19 #Vue.js
如何使用 vue-cli 创建模板项目
Nov 19 #Vue.js
深入了解Vue3模板编译原理
Nov 19 #Vue.js
You might like
PHP 字符串分割和比较
2009/10/06 PHP
php通过COM类调用组件的实现代码
2012/01/11 PHP
php class类的用法详细总结
2013/10/17 PHP
php cURL和Rolling cURL并发方式比较
2013/10/30 PHP
php使用json_encode对变量json编码
2014/04/07 PHP
PHP实现WebService的简单示例和实现步骤
2015/03/27 PHP
PHP设置Cookie的HTTPONLY属性方法
2017/02/09 PHP
php实现socket推送技术的示例
2017/12/20 PHP
你的编程语言可以这样做吗?
2006/09/07 Javascript
Javascript中的相等与不等运算
2010/04/25 Javascript
一些常用且实用的原生JavaScript函数
2010/09/08 Javascript
原生JS可拖动弹窗效果实例代码
2013/11/09 Javascript
jquery中load方法的用法及注意事项说明
2014/02/22 Javascript
jQuery动态添加及删除表单上传元素的方法(附demo源码下载)
2016/01/15 Javascript
Javascript的表单验证-提交表单
2016/03/18 Javascript
Angular中使用ui router实现系统权限控制及开发遇到问题
2016/09/23 Javascript
jQuery滑动到底部加载下一页数据的实例代码
2017/05/22 jQuery
json数据传到前台并解析展示成列表的方法
2018/08/06 Javascript
JS中使用react-tooltip插件实现鼠标悬浮显示框
2019/05/15 Javascript
vue2路由基本用法实例分析
2020/03/06 Javascript
Python wxPython库Core组件BoxSizer用法示例
2018/09/03 Python
英国领先的NHS批准的在线药店:Pharmacy2U
2017/01/06 全球购物
Sofmap官网:日本著名的数码电器专卖店
2017/05/19 全球购物
倩碧澳大利亚官网:Clinique澳大利亚
2019/07/22 全球购物
亿阳信通股份有限公司笔试题(C#)
2016/03/04 面试题
中学生在校期间的自我评价分享
2013/11/13 职场文书
幼儿园毕业园长感言
2014/02/24 职场文书
竞选生活委员演讲稿
2014/04/28 职场文书
商务英语专业毕业生求职信
2014/07/06 职场文书
高中升旗仪式演讲稿
2014/09/09 职场文书
自查自纠整改报告
2014/11/06 职场文书
党员转正介绍人意见
2015/06/03 职场文书
家庭经济困难证明
2015/06/23 职场文书
python - asyncio异步编程
2021/04/06 Python
css3实现背景图片颜色修改的多种方式
2021/04/13 HTML / CSS
Python中的协程(Coroutine)操作模块(greenlet、gevent)
2022/05/30 Python