ElementPlus el-table点击单元格变为输入框

开发项目的时候遇到一个要求是当点击el-table的单元格时要对应将其变为可输入的输入框,研究了文档和网上的博客后,实现了这个效果

首先是表格的html部分,通过插槽scope进行表格的自定义,方便后续的元素格式转换

<el-table :data="Table.printData.data" stripe :border="true">
	<el-table-column label="商品编号">
		<template #default="scope">
			<template v-if="scope.cellIndex == Table.cellIndex.value && scope.$index == Table.tableIndex.value">
                <el-input v-model="scope.row.goodsid" @blur="Table.saveCell(scope,scope.$index)" v-focus />
			</template>
	        <template v-else>
				<div class="box" @click="Table.editCell(scope, scope.$index)">{{ scope.row.goodsid }}
                </div>
		    </template>
		</template>
	</el-table-column>
	<el-table-column label="打印数量">
		<template #default="scope">
			<template v-if="scope.cellIndex == Table.cellIndex.value && scope.$index == Table.tableIndex.value">
			    <el-input v-model="scope.row.PrintCount" @blur="Table.saveCell" v-focus />
			</template>
			<template v-else>
				<div class="box" @click="Table.editCell(scope, scope.$index)">{{ scope.row.PrintCount }}</div>
			</template>
		</template>
	</el-table-column>
</el-table>

然后是function部分,通过传入的scope和scope.$index来确定单元格位置以及所处的表格行数,并定义两个行和格变量进行条件判断,当行和格的值等于单元格的行和高的值时就把那个单元格的元素改为input,并通过自定义指令v-focus进行焦点的自动获取,当焦点失去时,将变量的值还原,同时绑定的单元格的值也会被随之改变

printTable是数组内容,对应table的字段添加就好了,这里不赘述

let Table = {
printData:reactive({data:[]}),
cellIndex: ref(-1),
tableIndex: ref(-1),
editCell: function (row, ind) {
		Table.cellIndex.value = row.cellIndex;
		Table.tableIndex.value = ind;
    },
saveCell: function (type, id, index) {
		Table.cellIndex.value = -1;
		Table.tableIndex.value = -1;
	}
}

在main.ts文件注册自定义指令

app.directive('focus', {
	mounted(el) {
		el.querySelector('input').focus();
	}
});

到此就完成功能的开发了

  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
要实现在 `el-table` 点击按钮进行行编辑,您可以按照以下步骤操作: 1. 首先,在您的表格添加一个列,用于显示编辑按钮。您可以使用 `scoped slot` 来自定义列的内容。例如: ```html <el-table-column label="操作"> <template slot-scope="scope"> <el-button type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button> </template> </el-table-column> ``` 2. 然后,创建一个方法 `handleEdit`,该方法将触发行编辑的逻辑。在该方法,您可以通过修改数据源相应行的属性来切换编辑状态。例如: ```javascript methods: { handleEdit(row) { row.editable = true; // 添加一个表示可编辑状态的属性 } } ``` 3. 接下来,您可以使用 `scoped slot` 来根据行的编辑状态显示不同的内容。例如,在单元格显示不同的输入框或文本框。您可以根据需求使用 `v-if` 或 `v-show` 控制元素的显示与隐藏。例如: ```html <el-table-column label="姓名"> <template slot-scope="scope"> <span v-if="!scope.row.editable">{{ scope.row.name }}</span> <el-input v-else v-model="scope.row.name"></el-input> </template> </el-table-column> ``` 4. 最后,您可以添加保存或取消按钮,用于保存或取消对行数据的编辑。同样,您可以使用 `scoped slot` 来自定义列的内容。例如: ```html <el-table-column label="操作"> <template slot-scope="scope"> <el-button v-if="scope.row.editable" type="success" size="small" @click="handleSave(scope.row)">保存</el-button> <el-button v-else type="danger" size="small" @click="handleCancel(scope.row)">取消</el-button> </template> </el-table-column> ``` 请注意,在 `handleSave` 方法,您需要将修改后的数据保存到后端或修改您的数据源,具体操作根据您的需求而定。 这样,当您点击编辑按钮时,相应行的状态将切换为编辑状态,并显示相应的输入框或文本框。您可以在输入框进行编辑,并通过保存或取消按钮来确认或取消编辑。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值