首页 > 其他分享 >利用vue对数据进行增删改

利用vue对数据进行增删改

时间:2023-01-02 17:22:32浏览次数:44  
标签:userList vue userAge userImg userId userSex 利用 user 增删

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="js/vue.js"></script>
		<link href="css/userList.css" rel="stylesheet" />
		<style>
			button.hover {}
		</style>
	</head>
	<body>
		<div id="user">
			<table>
				<tr>
					<!-- 使用v-model来进行数据绑定,注意对应的元素名 -->
					<td>账号:</td>
					<td><input id="userId" v-model="user.userId" type="text" placeholder="ID" /></td>
				</tr>
				<tr>
					<td>年龄:</td>
					<td><input id="userAge" v-model="user.userAge" type="text" placeholder="0" /></td>
				</tr>
				<tr>
					<td>性别:</td>
					<!-- 对于单选框,要设置value,初始值应在vue对象中设置 -->
					<td><input v-model="user.userSex" type="radio" value="0" name="gender" />男
						<input v-model="user.userSex" type="radio" value="1" name="gender" />女
					</td>
				</tr>
				<tr>
					<td>图像:</td>
					<td><input id="userImg" v-model="user.userImg" type="text" /></td>
				</tr>
				<tr>
					<td><button @click="add()" v-if="user.flag==0">添加</button>
						<button @click="confirm()" v-if="user.flag==1">确定</button>
					</td>

				</tr>
			</table>
			<br />
			<table border="1px red solid" cellspacing="0px">
				<tr>
					<th class="th1">账号</th>
					<th class="th2">年龄</th>
					<th class="th3">性别</th>
					<th class="th4">图像</th>
					<th width="50px" colspan="2">操作</th>
				</tr>
				<!-- v-for将对象里面的数据循环取出装入到user,下面的index得到每个元素的索引值-->
				<tr id="idx" v-for="(user,index) in userList">
					<td class="th1">{{user.userId}}</td>
					<td class="th2">{{user.userAge}}</td>
					<td class="th3">{{user.userSex=="0"?"男":"女"}}</td>
					<td class="th4">
						<!-- 利用v-bind给标签的属性绑定数据 -->
						<img v-bind:src="user.userImg" width="50px" />
					</td>
					<td colspan="2">
						<button @click="deleteUser(index)">删除</button>
						<button @click="updateUser(index)">修改</button>
					</td>
				</tr>
			</table>
		</div>


		<script>
			var userIndex = 0;
			console.log(eval("025+12"))
			new Vue({
				el: "#user",
				data: {
					s: 12,
					user: {
						userId: "",
						userAge: "",
						userSex: "0",
						userImg: "img/pic2.png",
						flag: 0
					},
					userList: []
				},
				methods: {
					add() {
						if (this.user.userId == "") {
							alert("用户账号不能为空!")
							return
						}
						// 调用该函数的对象是vue,也就是说this指的是vue,但是我们此时要拿到user,将user装到userList里面
						let user = {
							userId: this.user.userId,
							userAge: this.user.userAge,
							userSex: this.user.userSex,
							userImg: this.user.userImg
						}
						this.userList.push(user)
					},
					// 点击按钮的时候我们拿到当前对象对应的索引值,根据索引来进行删除修改
					deleteUser(index) {
						this.userList.splice(index, 1)
					},
					updateUser(index) {
						// 在表单展示需要修改用户原本信息...
						this.user.userId = this.userList[index].userId
						this.user.userAge = this.userList[index].userAge
						var gender = document.getElementsByName("gender")
						if (this.userList[index].userSex == "0") {
							gender[1].checked = false;
							gender[0].checked = true
							this.user.userSex = 0
						} else {
							gender[1].checked = true;
							gender[0].checked = false
							this.user.userSex = 1
						}
						var userImg = document.getElementById("userImg")
						this.user.userImg = this.userList[index].userImg;
						this.user.flag = 1;
						userIndex = index
						console.log(userIndex)
					},
					confirm() {
						let user = {
							userId: this.user.userId,
							userAge: this.user.userAge,
							userSex: this.user.userSex,
							userImg: this.user.userImg
						}
						this.userList[userIndex] = user;
						//上面这些步骤完成了数据的修改,但是直接对数组进行修改不会重新渲染页面
						//直接修改data对象里面【页面需要存在】的值,页面会自动渲染
						this.user.flag=0;
					}
				},
				created() {
					//初始化数据
					this.userList = [{
						userId: "旋涡名人",
						userAge: 12,
						userSex: 1,
						userImg: "img/pic1.png"
					}, {
						userId: "更木剑八",
						userAge: 33,
						userSex: 1,
						userImg: "img/pic2.png"
					}, {
						userId: "卡卡西",
						userAge: 36,
						userSex: 1,
						userImg: "img/pic3.png"
					}, {
						userId: "小樱",
						userAge: 12,
						userSex: 0,
						userImg: "img/pic4.png"
					}, {
						userId: "小丸子",
						userAge: 12,
						userSex: 0,
						userImg: "img/pic5.png"
					}];
				}
			});
		</script>
	</body>
</html>

标签:userList,vue,userAge,userImg,userId,userSex,利用,user,增删
From: https://www.cnblogs.com/Liku-java/p/17020219.html

相关文章

  • 04使用Pinia实现Vuex项目全球化
    在上一章中使用的Vuex作为状态管理实现的全球化,这篇文章使用Pinia作为状态管理。现有用户可能对Vuex更熟悉,它是Vue之前的官方状态管理库。由于Pinia在生态系统中能......
  • aliyun baota 部署 ruoyi-vue
    nginx配置worker_processes1;events{worker_connections1024;}http{includemime.types;default_typeapplication/octet-stream;......
  • vue项目使用百度地图
    //b_map.jsexportfunctionMP(ak){  returnnewPromise(function(resolve,reject){   window.init=function(){    resolve(BMap); ......
  • 利用Useradd命令提权步骤
    利用Useradd命令提权步骤如果Shell用户的sudo权限中可以执行命令useradd,那么可以通过该命令实现提权。第一步:可以在KaliLinux上创建用户密码(假设要创建的用户名为jason......
  • 学vue的第二天:watch()事件侦听
    <!DOCTYPEhtml><html> <head> <metacharset="utf-8"> <title></title> <scriptsrc="js/vue.js"type="text/javascript"></script> <styletype="text/css">......
  • Vue案例——todolist
    最近在学习vue,实现todolist案例,实现效果如下:   该案例分为四个部分:header为输入框,body为列表,item是列表中的条目,footer为最下方的统计。实现步骤:①创建项目 v......
  • 校招前端二面高频vue面试题
    vue-router中如何保护路由分析路由保护在应用开发过程中非常重要,几乎每个应用都要做各种路由权限管理,因此相当考察使用者基本功。体验全局守卫:constrouter=createR......
  • vue为什么v-for的优先级比v-if的高?
    前言有时候有些面试中经常会问到v-for与v-if谁的优先级高,这里就通过分析源码去解答一下这个问题。下面的内容是在当我们谈及v-model,我们在讨论什么?的基础上分析的,所以......
  • vue的生命周期
    <!DOCTYPEhtml><html> <head> <metacharset="utf-8"/> <title></title> <scriptsrc="js/vue.js"type="text/javascript"charset="utf-8"></script> </head......
  • new Vue的时候到底做了什么
    Vue加载流程1.初始化的第一阶段是Vue实例也就是vm对象创建前后:首先Vue进行生命周期,事件初始化发生在beforeCreate生命周期函数前,然后进行数据监测和数据代理的初始化,也就......