vue使用element-ui实现table表格数据行内编辑

需求背景:鼠标双击编辑行内内容
实现思路:双击时获取到被点击的单元格元素,配合element内置方法,实现表单的显示与隐藏
当某表单内容显示时,自动获取光标,点击其他位置时表单会失去光标触发方法,其中一些方法是根据我自己当时的项目需要配合实现的,单独写了个相似的demo总结一下,还有很多可以改进的地方。

实现效果
在这里插入图片描述

html代码

<template>
	<div id="app">
		<!--  @cell-dblclick="tableDbEdit" 当单元格被双击击时会触发tableDbEdit事件,将文字变成input输入框-->
		<el-table ref="table" :data="tableList" border style="width: 100%" 
		@cell-dblclick="tableDbEdit">
			<el-table-column prop="title" label="标题">
				<template slot-scope="scope">
					<!-- 条件判断如果满足则显示表单,否则默认展示文字 -->
					<el-input type="textarea" size="small" 
					v-model="scope.row.title" 
					v-if="showInput == `title${scope.row.id}`" 
					@blur='blurInput(scope.row.id,"title",scope.row.title)'
					 v-focusTextarea>
					</el-input>
					<p v-else>{{scope.row.title}}</p>
				</template>
			</el-table-column>
			<el-table-column prop="name" label="姓名">
				<template slot-scope="scope">
					<el-input size="small" 
					v-model="scope.row.name" 
					v-if="showInput == `name${scope.row.id}`" 
					@blur='blurInput(scope.row.id,"name",scope.row.name)'
					 v-focus>
					</el-input>
					<p v-else>{{scope.row.name}}</p>
				</template>
			</el-table-column>
			<el-table-column label="品种" width="150" prop="type">
				<template slot-scope="scope">
					<el-input size="small" 
					v-model="scope.row.type" 
					v-if="showInput == `type${scope.row.id}`" 
					@blur='blurInput(scope.row.id,"type",scope.row.type)'
					 v-focus></el-input>
					<p v-else>{{scope.row.type}}</p>
				</template>
			</el-table-column>
		</el-table>
	</div>
</template>

javascript代码

<script>
	export default {
		data() {
			return {
				tableList: [{//表格数据
					id:0,
					name: '王皮皮',
					title: '在家小霸王,出门小王八',
					type: '中华田园猫'
				},{
					id:1,
					name: '王皮皮',
					title: '在家小霸王,出门小王八',
					type: '中华田园猫'
				},{
					id:2,
					name: '王皮皮',
					title: '在家小霸王,出门小王八',
					type: '中华田园猫'
				},{
					id:3,
					name: '王皮皮',
					title: '在家小霸王,出门小王八',
					type: '中华田园猫'
				}],
				showInput: '',
				oldData:{}
			}
		},
		directives: {
			// 通过自定义指令实现的表单自动获得光标的操作
			focus: {
				inserted: function (el) {
					if(el.tagName.toLocaleLowerCase() == 'input'){
						el.focus()
					}else{
						if(el.getElementsByTagName('input')){
							el.getElementsByTagName('input')[0].focus()
						}
					} 
					el.focus()
				}
			},
			focusTextarea: {
				inserted: function (el) {
					if(el.tagName.toLocaleLowerCase() == 'textarea'){
						el.focus()
					}else{
						if(el.getElementsByTagName('textarea')){
							el.getElementsByTagName('textarea')[0].focus()
						}
					} 
					el.focus()
				}
			}
		},
		// 方法
		methods: {
			// 当input失去光标后进行的操作
			async blurInput(id, name, value) {
				let obj = {}
				// 判断数据是否有所改变,如果数据有改变则调用修改接口
				if(this.oldData[name] != value){
					obj[name] = value//被改变的数据
					// 然后再写调用接口,提交内容的东西就可以了
				}
				this.showInput = ""
			},
			/*
			方法参数皆为框架方法的默认传参
			row 	行数据
			column	被点击的触发了方法的单元格
			event	事件
			*/
			tableDbEdit(row, column, event) {
				this.showInput = column.property + row.id
				this.oldData[column.property] = row[column.property]
			},
		}
	}
</script>
<style scoped>
	#app {
		width: 1000px;
		margin: 0 auto;
	}
</style>
  • 13
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值