我们将创建一个主组件App.vue
和一个子组件EditableCell.vue
来实现表格的编辑功能。
App.vue
<template>
<div class="table-layout">
<el-table :data="tableData">
<el-table-column type="index" label="序号" width="60" />
<el-table-column prop="id" label="ID" width="80" />
<el-table-column prop="date" label="日期">
<template #default="scope">
<EditableCell
:cell-data="scope.row.date"
:is-editing="scope.row.isEditDate"
:column="scope.column.property"
:row="scope.row"
@toggleEdit="toggleEdit"
@applyChanges="applyChanges"
/>
</template>
</el-table-column>
<el-table-column prop="name" label="名称">
<template #default="scope">
<EditableCell
:cell-data="scope.row.name"
:is-editing="scope.row.isEditName"
:column="scope.column.property"
:row="scope.row"
@toggleEdit="toggleEdit"
@applyChanges="applyChanges"
/>
</template>
</el-table-column>
<el-table-column prop="description" label="描述">
<template #default="scope">
<EditableCell
:cell-data="scope.row.description"
:is-editing="scope.row.isEditDescription"
:column="scope.column.property"
:row="scope.row"
@toggleEdit="toggleEdit"
@applyChanges="applyChanges"
/>
</template>
</el-table-column>
</el-table>
</div>
</template>
<script setup>
import { ref } from 'vue';
import EditableCell from './EditableCell.vue'; // 确保路径正确
import { ElTable, ElTableColumn, ElInput, ElIcon } from 'element-plus';
import { Edit } from '@element-plus/icons-vue';
// 优化后的tableData
const tableData = ref([
{
id: 1,
date: '2024-08-01',
name: '项目A',
description: '这是一个关于项目A的描述',
isEditDate: false,
isEditName: false,
isEditDescription: false
},
{
id: 2,
date: '2024-08-15',
name: '项目B',
description: '这是一个关于项目B的描述',
isEditDate: false,
isEditName: false,
isEditDescription: false
},
{
id: 3,
date: '2024-09-01',
name: '项目C',
description: '这是一个关于项目C的描述',
isEditDate: false,
isEditName: false,
isEditDescription: false
}
]);
function toggleEdit(column, row) {
if (column && row) {
const editKey = `isEdit${column.charAt(0).toUpperCase() + column.slice(1)}`;
row[editKey] = !row[editKey];
}
}
function applyChanges(newValue, column, row) {
if (column && row) {
row[column] = newValue;
const editKey = `isEdit${column.charAt(0).toUpperCase() + column.slice(1)}`;
row[editKey] = false;
}
}
</script>
<style scoped>
.table-layout {
width: 60%;
margin: 60px auto;
}
.edit-icon {
margin: 10px 0 0 10px;
}
</style>
EditableCell.vue
1<template>
2 <div>
3 <span v-if="!isEditing">{{ displayData }}</span>
4 <el-input
5 v-else
6 v-model="displayData"
7 style="width: 120px;"
8 @blur="onBlur"
9 />
10 <el-icon color="#409EFF" class="edit-icon" @click="onToggleEdit">
11 <Edit />
12 </el-icon>
13 </div>
14</template>
15
16<script>
17export default {
18 name: 'EditableCell',
19 props: ['cellData', 'isEditing', 'column', 'row'], // 添加row属性
20 emits: ['toggleEdit', 'applyChanges'],
21 data() {
22 return {
23 displayData: this.cellData
24 };
25 },
26 watch: {
27 cellData(newVal) {
28 this.displayData = newVal;
29 }
30 },
31 methods: {
32 onToggleEdit() {
33 this.$emit('toggleEdit', this.column, this.row); // 传递column和row
34 },
35 onBlur() {
36 this.$emit('applyChanges', this.displayData, this.column, this.row); // 传递column和row
37 }
38 }
39}
40</script>
41
42<style scoped>
43.edit-icon {
44 margin: 10px 0 0 10px;
45}
46</style>
子组件 (EditableCell.vue
):
displayData
来存储编辑时的值,并通过watch
确保它与cellData
同步。onBlur
方法中提交更改,并正确调用applyChanges
方法。父组件 (App.vue
):
scope.column.property
获取列的属性名称,并将其传递给子组件。scope.row
将行数据传递给子组件。toggleEdit
方法中,通过构造的editKey
来切换编辑状态。applyChanges
方法中,通过构造的editKey
来更新编辑状态,并保存新值。通过这种方式,我们实现了表格的编辑功能,并确保了编辑状态下数据的正确提交。
本文介绍了如何使用Vue 3和Element Plus实现一个带有编辑功能的表格。通过本文的步骤,你可以轻松地在自己的项目中实现类似的表格编辑功能。希望这篇教程对你有所帮助!