VUE+Element实现增删改查的示例源码
作者:古有风情·月下谈之 发布时间:2024-05-09 09:32:47
前言
&最近因为一些原因,没有更博客,昨天老师布置了一个作业,用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>
来源:https://www.cnblogs.com/hxz0618/p/14017583.html?utm_source=tuicool&utm_medium=referral


猜你喜欢
- 前言在进行接口测试时,有些接口字段在不需要测试的时候往往是被写死的,但是你不能保证它就不会出现问题,所以在平时测试的时候就需要覆盖各种情况,
- 稀疏矩阵-sparsepfrom scipy import sparse稀疏矩阵的储存形式在科学与工程领域中求解线性模型时经常出现许多大型的
- 安装环境:CentOS7 64位 MINI版官网源码编译安装文档:http://dev.mysql.com/doc/refman/5.7/e
- 前言后端开发中为了防止一次性加载太多数据导致内存、磁盘IO都开销过大,经常需要分页展示,这个时候就需要用到MySQL的LIMIT关键字。但你
- 如下所示:# -*- coding: utf-8 -*-# @Time : 2018/5/17 15:05# @Author :
- 一、需求描述文本溢出省略,说实话这些年也实践过很多了,这次是针对富文本字符串,思量想去,也曾试图了解一些知名站点的实现方案,但结果不甚理想。
- 1.什么是gRPCgRPC是rpc框架中的一种,是rpc中的大哥是一个高性能,开源和通用的RPC框架,基于Protobuf序列化协议开发,且
- 学习了一天的深度学习,略有疲惫,我们用pygame搞个小游戏放松放松吧。今天我们的游戏主体是烟雨蒙蒙下彩虹雨,仿佛置身江南水乡。游戏描述我们
- 项目场景:最近在部署项目之后,运行出现报错:Expression #1 of SELECT list is not in GROUP BY
- # django manage.py扩展自定义命令环境: mac django1.10.3在实际的项目开发过程中,我们可能要执行某脚本初始化
- 一、问题描述通过调用MyQR模块来实现生成个人所需二维码。安装:pip install myqr二、代码实现1.普通二维码from MyQR
- 要开发一个基于数据库的应用系统,其中最关键的一步就是整个系统所依据的数据库的建模设计,从逻辑的到物理的,一个环节疏于设计,整个的应用系统便似
- 如下:counter.htm<a href=counter.asp?save=123&url=http://127.0.0
- 本文实例讲述了Go语言判断文件或文件夹是否存在的方法。分享给大家供大家参考,具体如下:Golang 判断文件是否存在有点怪异,是根据在操作文
- MySQL由于它本身的小巧和操作的高效, 在数据库应用中越来越多的被采用.我在开发一个P2P应用的时候曾经使用MySQL来保存P2P节点,由
- 每位SQL Server开发员都有自己的首选操作方法。我的方法叫做分子查询。这些是由原子查询组合起来的查询,通过它们我可以处理一个表格。将原
- 交待:使用的软硬件环境为Win XP SP2、SQL Server 2000 SP2个人版、普通双核台式机、1000M局域网,A机为已使用的
- 前言:如何将一个JSON文档映射为Python对象主要包括一下三个部分:考点:loads函数的用法。面试题:如何将一个JSON文档映射为Py
- 某天气网站(www.数字.com)存有2011年至今的天气数据,有天看到一本爬虫教材提到了爬取这些数据的方法,学习之,并加以改进。准备爬的历
- 我们在做自动化运维的时候,经常需要调用api中的接口,不过很多人不知道具体的调用方法,在学习python中的requests库后,我们就可以