首页 > 其他分享 >Vue实现表格的增删改查

Vue实现表格的增删改查

时间:2023-02-14 12:00:44浏览次数:40  
标签:Vue name 改查 sex item add 增删 hobby email

<iframe allowfullscreen="true" data-mediaembed="bilibili" src="https://player.bilibili.com/player.html?aid=92821320"></iframe>

bandicam 2020-03-01 09-41-46-926


html

<body>
		<div id="app">
			<model ref='mod' v-show=isShow @addlist="addItem" @editlist='editDate' @cancle='cancle' :passvalue="c"></model>
			<datalist id="data">
				<option v-for="list in search(keys)">{{ list.name}}</option>
			</datalist>
			<input type="text" placeholder="关键字查询" class="search" list="data" v-model="keys">
			<button class="add" @click="add">添加</button>
			<table cellspacing="0">
				<thead>
					<tr>
						<th style="border-top-left-radius: 10px;">Id</th>
						<th>Name</th>
						<th>Sex</th>
						<th>Email</th>
						<th>Address</th>
						<th>Hobby</th>
						<th width="150px" style="border-top-right-radius: 10px;">Operation</th>
					</tr>
				</thead> 
				<tbody>
					<tr @mouseenter="high(index)" @mouseleave="currentIndex=-1" v-for="(item,index) in search(keys)" :class="[index==currentIndex?highlight:'']">
						<td>{{ index+1}}</td>
						<td>{{ item.name}}</td>
						<td>{{ item.sex}}</td>
						<td>{{ item.email}}</td>
						<td>{{ item.address}}</td>
						<td>{{ item.hobby}}</td>
						<td><a href="JavaScript:void(0);" @click="remove(index)">删除</a> <span @click="change(index)" :class="[index==currentIndex?edit:'']"></span></td>
					</tr>
				</tbody>
			</table>
		</div>
		<script src="vue增删改查demo.js"></script>
	</body>

js

Vue.component('model', {
	template: `<div style='position:relative'  @update="changeValue">
	<div class='floor'></div>
	<div class='modify'>
	<span>Name:</span><input type='text' v-model='setName' /><br>
	<span>Sex:</span>&nbsp;&nbsp;&nbsp;&nbsp;<input type='radio' v-model='setSex' value='男' />男<input type='radio' value='女' v-model='setSex' />女 <input type='radio' value='未知' v-model='setSex' />未知 <br>
	<span>Email:</span>&nbsp;<input type='text' v-model='setEmail' /><br>
	<span>Address:</span><select class='select' v-model='AddrSelect'>
		<option size='4'>北京</option>
		<option>浙江</option>7
		<option>朝阳</option>
		<option>西安</option>
		<option>深圳</option>
		<option>自定义</option>
	</select><input type='text' class='dryAddr' v-if=dryAddr  v-model='AddrSelect' /><br>
	<span>Hobby</span>:
	<input type='checkbox' v-model='setHobby' value='唱' />唱
	<input type='checkbox' v-model='setHobby' value='跳' />跳
	<input type='checkbox' v-model='setHobby' value='rap' />rap
	<input type='checkbox' v-model='setHobby' value='篮球' />篮球
	<input type='checkbox' v-model='setHobby' value='嚼摈榔' />嚼摈榔
	<input type='checkbox' v-model='setHobby' value='要棒棒糖' />要棒棒糖
	<input type='checkbox' v-model='setHobby' value='giao' />giao
	<input type='checkbox' v-model='setHobby' value='cbb' />cbb
	<input type='checkbox' v-model='setHobby' value='奥力给'/>奥力给
	<input type='checkbox' v-model='setHobby' value='正能量语录' />正能量语录
	<input type='checkbox' v-model='setHobby' value='游泳' />游泳
	<input type='checkbox' v-model='setHobby' value='优质吃播' />优质吃播
	<br>
	<button style='margin-left:120px' @click='test'>取消</button>
	<button @click='save' v-show='isSave'>保存</button>
	<button @click='upDate' v-show='!isSave'>更新</button>
	</div>
	</div>`,
	data() {
		return {
			dryAddr: false,
			AddrSelect: '北京',
			setName: '',
			setSex: '',
			setEmail: '@qq.com',
			setHobby: [],
			add: {
				name: '',
				sex: '',
				email: '',
				address: '',
				hobby: ''
			},
			quote: {},
			isSave: true,
			id:'',
		}
	},
	props: {
		passvalue: Object,
	},
	methods: {
		test() {
			this.$emit('cancle');
			this.AddrSelect = '北京'
			this.setName = ''
			this.setSex = ''
			this.setEmail = '@qq.com'
			this.setHobby = []
		},
		changeValue() {
			this.$nextTick(() => {
				this.AddrSelect = this.passvalue.address;
				this.setName = this.passvalue.name;
				this.setSex = this.passvalue.sex;
				this.setEmail = this.passvalue.email;
				this.setHobby = this.passvalue.hobby.split(',');
				this.id = this.passvalue.id;
			});
			this.isSave = false;
		},
		save() {
			this.add.name = this.setName;
			this.add.sex = this.setSex;
			this.add.email = this.setEmail;
			this.add.address = this.AddrSelect;
			this.add.hobby = this.setHobby.toString();
			this.$emit('addlist', this.add);
			this.setName = '';
			this.setSex = [];
			this.setEmail = '@qq.com';
			this.AddrSelect = '北京';
			this.setHobby = [];
		},
		upDate() {
			this.add.name = this.setName;
			this.add.sex = this.setSex;
			this.add.email = this.setEmail;
			this.add.address = this.AddrSelect;
			this.add.hobby = this.setHobby.toString();
			this.$emit('editlist', this.add,this.id);
			this.setName = '';
			this.setSex = [];
			this.setEmail = '@qq.com';
			this.AddrSelect = '北京';
			this.setHobby = [];
			this.isSave = true;
		}
	},
	watch: {
		AddrSelect(v, oldv) {
			if (v == '自定义') {
				this.dryAddr = true
			}
		}
	}

})

let vm = new Vue({
	el: "#app",
	data() {
		return {
			list: [{
					id: 1,
					name: '坤坤',
					sex: '未知',
					address: '浙江',
					email: '[email protected]',
					hobby: "唱,跳,rap,篮球"
				},
				{
					id: 2,
					name: '啊giao',
					sex: '男',
					address: '西安',
					email: '[email protected]',
					hobby: "嚼摈榔,要棒棒糖,giao"
				},
				{
					id: 3,
					name: '朝阳泳怪',
					sex: '男',
					address: '朝阳',
					email: '[email protected]',
					hobby: "游泳,正能量语录,奥力给"
				},
				{
					id: 4,
					name: '老八',
					sex: '男',
					address: '沈阳',
					email: '[email protected]',
					hobby: "cbb,优质吃播"
				}
			],
			keys: '',
			highlight: 'highlight',
			edit: 'edit',
			currentIndex: -1,
			isShow: false,
			o: {},
			c: {},
		}
	},
	methods: {
		search(keys) {
			return this.list.filter(item => {
				if (item.name.includes(keys)) {
					return item;
				};

			})
		},
		remove(index) {
			this.list.splice(index, 1)
		},
		high(index) {
			this.currentIndex = index;
		},
		add() {
			this.isShow = true
		},
		addItem(item) {
			this.isShow = false;
			//这无法使用this.list.push(item)添加,因为浅拷贝,下次提价数据会把前面的数据也给改变了
			this.o = {
				name: item.name,
				sex: item.sex,
				email: item.email,
				address: item.address,
				hobby: item.hobby,
			};

			this.list.push(this.o);
		},
		editDate(item,index){
			this.isShow = false;
			this.o = {
				name: item.name,
				sex: item.sex,
				email: item.email,
				address: item.address,
				hobby: item.hobby,
			};
			this.list.splice(index,1,this.o);
		},
		cancle() {
			this.isShow = false;
		},
		change(index) {
			this.c = this.list[index];
			this.c.id = index;
			this.isShow = true;
			this.$refs.mod.changeValue();
		}
	}
})

标签:Vue,name,改查,sex,item,add,增删,hobby,email
From: https://blog.51cto.com/u_15964288/6056544

相关文章

  • Vue props中的default值正确写法
    先看一个警告[Vuewarn]:Invaliddefaultvalueforprop"content":PropswithtypeObject/Arraymustuseafactoryfunctiontoreturnthedefaultvalue.//错......
  • VUE 项目大文件上传下载解决方案
    ​ 一、基本介绍 1,什么是WebUploader?WebUploader是由百度公司团队开发的一个以HTML5为主,FLASH为辅的现代文件上传组件。官网地址:http://fex.baidu.com/webuploa......
  • VsCode运行VUE3出现错误
        1.首先查看vscode版本2.安装最新稳定的nodejs可以在注册表查看一些有效或者无效的路径  可以cmd查看node-v  和 npm-v3.如果出现一些提示vite......
  • vue原理:diff、模板编译、渲染过程等
    一、虚拟DOM:因为DOM操作非常消耗性能,在操作DOM时,会出现DOM的回流(Reflow:元素大小或者位置发生改变)与重绘(元素样式的改变)使DOM重新渲染。现在的框架Vue和React很少直接操作......
  • vue 锚点定位和置顶
    锚点定位//vue中使用//标题<divclass="tabs"v-for="(item,index)intitAll":key="index":class="{actives:isactive===index}"@click="ta......
  • Vue3 hash and history mode
    今天在修改hash模式为history模式的时候,发现页面在重新刷新后直接显示404了。多方排查,发现vue.config.js中有配置publicPath:'./',此配置在hash模式下是成功的,但是当......
  • vue中兄弟组件传值event bus被多次触发以及踩过的坑
    传值方式:兄弟组件传值的时候,使用的是this.$bus.$emit触发事件和this.$emit.on监听事件。业务bug:点击按钮切换兄弟组件,再切换的过程$eventBus出发了多次。原因:事件订......
  • vue基础
     vue是一套用于构建用户界面的渐进式(vue可以自底向上逐层的应用;由一个轻量小巧的核心库到可以引入各式各样的vue插件)JavaScript框架。 vue的特点:1.采用组件化模式......
  • git bash窗口创建vue项目, 箭头切换没起作用?
    1、解决方法:一、直接在命令窗口创建(Win+R),输入cmd二、使用winpty命令+.cmd,如:winptynpm.cmdcreatevite@latest  ......
  • vue为什么v-for的优先级比v-if的高?
    前言有时候有些面试中经常会问到v-for与v-if谁的优先级高,这里就通过分析源码去解答一下这个问题。下面的内容是在当我们谈及v-model,我们在讨论什么?的基础上分析的,所以......