欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 名人名企 > el-table 单元格,双击编辑

el-table 单元格,双击编辑

2024/10/24 20:18:58 来源:https://blog.csdn.net/weixin_43159039/article/details/141957672  浏览:    关键词:el-table 单元格,双击编辑

el-table 单元格,双击编辑

实现效果

在这里插入图片描述

代码如下

<template><el-table :data="tableData" style="width: 100%"><el-table-column prop="name" label="姓名" width="180"><template slot-scope="scope"><div @dblclick="editCell(scope, 'name')" style="cursor: pointer;"><span v-if="!isEditingCell || editingCell.row !== scope.row || editingCell.field !== 'name'">{{ scope.row.name }}</span><el-inputv-elsev-model="editingCell.value"@blur="saveEdit(scope.row, 'name')"@keydown.enter="saveEdit(scope.row, 'name')"size="small"style="width: 100px;"/></div></template></el-table-column><el-table-column prop="age" label="年龄" width="180"><template slot-scope="scope"><div @dblclick="editCell(scope, 'age')" style="cursor: pointer;"><span v-if="!isEditingCell || editingCell.row !== scope.row || editingCell.field !== 'age'">{{ scope.row.age }}</span><el-inputv-elsev-model="editingCell.value"@blur="saveEdit(scope.row, 'age')"@keydown.enter="saveEdit(scope.row, 'age')"size="small"style="width: 100px;"/></div></template></el-table-column></el-table>
</template><script>
export default {data() {return {tableData: [{ name: 'John', age: 25 },{ name: 'Jane', age: 30 },{ name: 'Doe', age: 28 },],isEditingCell: false,editingCell: {row: null,field: null,value: null,},};},methods: {editCell(scope, field) {this.isEditingCell = true;this.editingCell.row = scope.row;this.editingCell.field = field;this.editingCell.value = scope.row[field]; // 将原有的值赋给编辑单元格},saveEdit(row, field) {row[field] = this.editingCell.value;  // 将输入的值保存回原数据this.isEditingCell = false;this.editingCell.row = null;this.editingCell.field = null;this.editingCell.value = null; // 清空编辑值},},
};
</script><style scoped>
/* 自定义样式 */
</style>

如果值为空无法点击无效的话,可以设置<span>的样式 style="display: inline-block ;

<span v-if="!isEditingCell || editingCell.row !== scope.row || editingCell.field !== 'age'" style="display: inline-block ;>{{ scope.row.age }}</span>

这样就不会遇到值为空,但是无法显示的情况了

具体的内容大家可以根据实际需要调整!

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com