VUE+Element实现增删改查

@


前言

&最近因为一些原因,没有更博客,昨天老师布置了一个作业,用vue实现增删改查功能,想想这也不难,就做一下试试吧。

因为自己写的样式没有别人做的好,因此我想用现成的UI框架,一直也没用过Element,就干脆趁机学一下吧。

实验步骤

  1. 首先引入一下element的css以及js
<!-- 引入样式 -->
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
<!-- 引入组件库 -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
  1. 然后引入需要用到的vue相关的js文件
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  1. 下面说一下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操作 后面需要用到

4. 加上基础的样式

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

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

  1. 下面开始写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">
<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实现增删改查(内附源代码)的更多相关文章

  1. vue的增删改查

    我们把这些用户信息保存到list的数组中,然后增删改查就在这个数组上进行: list: [ { username: 'aaaaa', email: '123@qq.com', sex: '男', pr ...

  2. 关于vue的增删改查操作

    利用vue也可以实现数据的增删改查,只是未涉及到数据库,只是在浏览器页面中进行操作. 将datas数组中的数据循环输出: 再增加一行,用于保存新数据,编辑数据后保存: 此时,数据已经呈现出来,开始进行 ...

  3. webpack4+express+mongodb+vue 实现增删改查

    在讲解之前,我们先来看看效果如下所示: 1)整个页面的效果如下: 2) 新增数据效果如下: 3) 新增成功如下: 4) 编辑数据效果如下: 5) 编辑成功效果如下: 6) 删除数据效果如下: 7) 删 ...

  4. Javaweb实现对mongodb的增删改查(附带源代码)

    运行截图: 删除后的信息 项目源代码:https://www.cnblogs.com/post/readauth?url=/zyt-bg/p/9807396.html

  5. Vue实现增删改查功能

    简单的表单CURD功能demo <!DOCTYPE html> <html> <head> <meta charset="UTF-8"&g ...

  6. MyBatis项目配置案例详解与Web下的增删改查实现[附项目源码]

    MyBatis项目案例 项目图示: 项目源码地址:https://github.com/JluTiger/mybatispro 1.项目功能 项目案例:后台管理系统用户数据维护平台 所有用户数据查询 ...

  7. 利用Java针对MySql封装的jdbc框架类 JdbcUtils 完整实现(包括增删改查、JavaBean反射原理,附源代码)

    近期看老罗的视频,跟着完毕了利用Java操作MySql数据库的一个框架类JdbcUtils.java,完毕对数据库的增删改查.当中查询这块,包含普通的查询和利用反射完毕的查询,主要包含以下几个函数接口 ...

  8. JS组件系列——又一款MVVM组件:Vue(一:30分钟搞定前端增删改查)

    前言:关于Vue框架,好几个月之前就听说过,了解一项新技术之后,总是处于观望状态,一直在犹豫要不要系统学习下.正好最近有点空,就去官网了解了下,看上去还不错的一个组件,就抽空研究了下.最近园子里vue ...

  9. VUE2.0增删改查附编辑添加model(弹框)组件共用

    Vue实战篇(增删改查附编辑添加model(弹框)组件共用) 前言 最近一直在学习Vue,发现一份crud不错的源码 预览链接 https://taylorchen709.github.io/vue- ...

随机推荐

  1. lumen单元测试

    phpunit --filter testInfo  tests/UserTest.php UserTest.php <?php use Laravel\Lumen\Testing\Databa ...

  2. jdk可视化工具系列——检视阅读

    jdk可视化工具系列--检视阅读 参考 java虚拟机系列 RednaxelaFX知乎问答 RednaxelaFX博客 JConsole--Java监视与管理控制台 jconsole介绍 JConso ...

  3. Vue内容

    vue中的过滤器 moeths点击 过滤器的含义 过滤器就是把原有的数据过一遍 放到页面中  不会改变原有的数据   只是在原有的数据上增加新的数据

  4. rpc服务在游戏中的简单运用

    我们最开始做的游戏框架,多数都是client->server->db的模式,但是随着玩家数量的增加,一个server进程就会扛不住,需要多个进程服务于多个玩家.但是给定了不同进程的玩家,有 ...

  5. ArrayBlockingQuque摘要

    ArrayBlockingQuque 优势 线程同步,线程安全 对应空或满时,take\put操作将阻塞 内部是一个数组,每个元素不会产生额外的处理对象,如Node 基于什么 ReentrantLoc ...

  6. java前后端开发需掌握的框架及技术

    一.Java开发 1.J2EE架构及主流框架,spring4.spring boot.spring MVC.spring Security.spring cloud.struct2.hibernate ...

  7. (or type Control-D to continue):

    (or type Control-D to continue): 很多小伙伴学习使用Linux时可能经常遇到这个问题 (大部分原因是磁盘挂载等问题) 如下图: 具体解决方法 1.直接输入root用户的 ...

  8. GPRS DTU设备常见的问题分析

    在GPRS DTU设备使用的过程中,经常会遇到各种各样的问题,今天就为大家来分析一下在GPRS DTU设备使用时会遇到的一些问题. 1.GPRS模块设置 a.检查串口参数是否与GPRS模块的工作参数一 ...

  9. Java中final修饰的方法是否可以被重写

    这是一次阿里面试里被问到的题目,在我的印象中,final修饰的方法是不能被子类重写的.如果在子类中重写final修饰的方法,在编译阶段就会提示Error.但是回答的时候还是有点心虚的,因为final变 ...

  10. error: invalid command ‘bdist_wheel‘

    解决方法: pip3 install wheel 了解更多,请关注公众号