vue实现li新增修改删除

4 篇文章 0 订阅

在这里插入图片描述
在这里插入图片描述在这里插入图片描述在这里插入图片描述

html

<div class="info oneBox">
							<p>前端html</p>
							<div v-for="item in jsyjList" class="oneBoxlist">
								<span v-if="item.id != isXg" :id="item.id">{{ item.name }}</span>
								<input type="text" placeholder="请输入" :id="item.id" v-model="item.name"
									v-if="item.id == isXg" @blur="overxg(item.id)" />
								<div class="oneBoxpic">
									<img src="../../assets/icon/bj2.png" title="编辑" @click="exit(item.id)" />
									<img src="../../assets/icon/del2.png" title="删除" @click="del(item.id)" />
								</div>
							</div>
							<div style="text-align:right;">
								<el-button size="mini" @click="addlist" style="padding:4px 10px;">
									<img src="../../assets/add.png" style="width:26px;" />
								</el-button>
							</div>
						</div>

js

//data
data:{
isXg: '',
jsyjList: [],
}
//新增
		addlist() {
			this.jsyjList.push({ name: '', id: uuidv4() },);
			let idLast = this.jsyjList[this.jsyjList.length - 1].id
			this.isXg = idLast;
			this.$nextTick(() => {
				var txtInput = document.getElementById(idLast);
				txtInput.focus();
			})

		},
		//编辑
		exit(id) {
			this.isXg = id;
			this.$nextTick(() => {
				var txtInput = document.getElementById(id);
				txtInput.focus();
			})
		},
		//删除
		del(id) {
			this.jsyjList.forEach(function (val, index, arr) {
				if (val.id == id) {
					arr.splice(index, 1);
				}
			})
		},
		// 如果输入为空,则取消新增
		overxg(id) {
			this.isXg = '';
			this.$nextTick(() => {
				var valInput = document.getElementById(id).innerText;
				if (valInput == '') {
					this.jsyjList.forEach(function (val, index, arr) {
						if (val.id == id) {
							arr.splice(index, 1);
						}
					})
				}
			})
		},

css

.oneBox p {
	width: calc(100% - 150px);
}

.oneBoxlist {
	display: flex;
	align-items: center;
}

.oneBoxlist:hover .oneBoxpic img {
	display: block;
}

.oneBoxlist span {
	line-height: 30px;
	font-size: 14px;
	color: #606266;
	text-indent: 2rem;
	display: inline-block;
	max-width: calc(100% - 100px);
}

.oneBoxpic {
	height: 30px;
	display: flex;
	align-items: center;
	width: 45px;
	margin-left: 10px;
}

.oneBoxlist .oneBoxpic img {
	width: 20px;
	height: 20px;
	display: none;
}

.oneBoxlist input {
	border: 0;
	width: calc(100% - 132px);
	padding: 4px 2px;
	height: 36px;
	margin-left: 32px;
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值