欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Vue element table点击单元格可编辑

程序员文章站 2022-06-08 14:59:05
...

1、

<el-table :data="tableData" @cell-click="cellClick"
			:cell-class-name="getRowColumn">
	<el-table-column prop="note" align="center" label="列1" >
		<template  slot-scope="scope">
			<el-input v-if="scope.row.index==clickCellRowIndex && scope.column.index==clickCellColumnIndex" size="small" v-model="scope.row.note" @blur="inputBlur" 
            				placeholder="请输入">
			</el-input>
			<span v-else>{{scope.row.note}}</span>
		</template>
	</el-table-column>
</el-table>

2、

data(){
	return {
		clickCellRowIndex:null,//用于判断点击是哪行
		clickCellColumnIndex:null,//用于判断点击是哪列
	}
}

3、

methods{
	//输入框 失去焦点
	inputBlur(index,row){
		this.clickCellRowIndex = null
		this.clickCellColumnIndex = null

	},
	//点击cell
	cellClick(row,column,cell,event){
		this.clickCellRowIndex = row.index
		this.clickCellColumnIndex = column.index

	},
	//table初始化的时候给行和列 赋index
	getRowColumn({row,column,rowIndex,columnIndex}){
		row.index = rowIndex
		column.index = columnIndex
	},
}