Vue 3 + Element Plus 封裝單列控制編輯的可編輯表格組件的方法
在Web應(yīng)用開(kāi)發(fā)中,經(jīng)常需要提供表格數(shù)據(jù)的編輯功能。本文將介紹如何使用Vue 3結(jié)合Element Plus庫(kù)來(lái)實(shí)現(xiàn)一個(gè)支持單列控制編輯功能的表格,并通過(guò)封裝組件的形式提高代碼的復(fù)用性。通過(guò)本教程,你將學(xué)會(huì)如何構(gòu)建一個(gè)具備單列控制編輯功能的表格組件,并在表單提交時(shí)保存更改。

目標(biāo)
- 實(shí)現(xiàn)一個(gè)基本的表格,其中包含日期、名稱和描述等列。
- 表格中的每一項(xiàng)都可以在點(diǎn)擊編輯圖標(biāo)后進(jìn)入編輯模式。
- 編輯模式下可以修改表格單元格的內(nèi)容。
- 編輯完成后,可以通過(guò)失去焦點(diǎn)的方式提交更改。
- 支持單列控制編輯,即可以單獨(dú)控制每一列表格單元格的編輯狀態(tài)。
- 封裝成可復(fù)用的組件,便于在其他項(xiàng)目中使用。
創(chuàng)建表格組件
我們將創(chuàng)建一個(gè)主組件App.vue和一個(gè)子組件EditableCell.vue來(lái)實(shí)現(xiàn)表格的編輯功能。
主組件 App.vue
<template>
<div class="table-layout">
<el-table :data="tableData">
<el-table-column type="index" label="序號(hào)" 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';
// 優(yōu)化后的tableData
const tableData = ref([
{
id: 1,
date: '2024-08-01',
name: '項(xiàng)目A',
description: '這是一個(gè)關(guān)于項(xiàng)目A的描述',
isEditDate: false,
isEditName: false,
isEditDescription: false
},
{
id: 2,
date: '2024-08-15',
name: '項(xiàng)目B',
description: '這是一個(gè)關(guān)于項(xiàng)目B的描述',
isEditDate: false,
isEditName: false,
isEditDescription: false
},
{
id: 3,
date: '2024-09-01',
name: '項(xiàng)目C',
description: '這是一個(gè)關(guān)于項(xiàng)目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
<template>
<div>
<span v-if="!isEditing">{{ displayData }}</span>
<el-input
v-else
v-model="displayData"
style="width: 120px;"
@blur="onBlur"
/>
<el-icon color="#409EFF" class="edit-icon" @click="onToggleEdit">
<Edit />
</el-icon>
</div>
</template>
<script>
export default {
name: 'EditableCell',
props: ['cellData', 'isEditing', 'column', 'row'], // 添加row屬性
emits: ['toggleEdit', 'applyChanges'],
data() {
return {
displayData: this.cellData
};
},
watch: {
cellData(newVal) {
this.displayData = newVal;
}
},
methods: {
onToggleEdit() {
this.$emit('toggleEdit', this.column, this.row); // 傳遞column和row
},
onBlur() {
this.$emit('applyChanges', this.displayData, this.column, this.row); // 傳遞column和row
}
}
}
</script>
<style scoped>
.edit-icon {
margin: 10px 0 0 10px;
}
</style>說(shuō)明
子組件 (EditableCell.vue):
- 使用內(nèi)部狀態(tài)
displayData來(lái)存儲(chǔ)編輯時(shí)的值,并通過(guò)watch確保它與cellData同步。 - 在模板中,根據(jù)編輯狀態(tài)顯示不同的內(nèi)容。
- 在
onBlur方法中提交更改,并正確調(diào)用applyChanges方法。
父組件 (App.vue):
- 在模板中,通過(guò)
scope.column.property獲取列的屬性名稱,并將其傳遞給子組件。 - 在模板中,通過(guò)
scope.row將行數(shù)據(jù)傳遞給子組件。 - 在
toggleEdit方法中,通過(guò)構(gòu)造的editKey來(lái)切換編輯狀態(tài)。 - 在
applyChanges方法中,通過(guò)構(gòu)造的editKey來(lái)更新編輯狀態(tài),并保存新值。
通過(guò)這種方式,我們實(shí)現(xiàn)了表格的編輯功能,并確保了編輯狀態(tài)下數(shù)據(jù)的正確提交。
總結(jié)
本文介紹了如何使用Vue 3和Element Plus實(shí)現(xiàn)一個(gè)帶有編輯功能的表格。通過(guò)本文的步驟,你可以輕松地在自己的項(xiàng)目中實(shí)現(xiàn)類似的表格編輯功能。希望這篇教程對(duì)你有所幫助!
到此這篇關(guān)于Vue 3 + Element Plus 封裝單列控制編輯的可編輯表格組件的文章就介紹到這了,更多相關(guān)Vue 3 Element Plus 編輯表格組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue3實(shí)現(xiàn)動(dòng)態(tài)路由與手動(dòng)導(dǎo)航
這篇文章主要為大家詳細(xì)介紹了如何在?Vue?3?中實(shí)現(xiàn)動(dòng)態(tài)路由注冊(cè)和手動(dòng)導(dǎo)航,確保用戶訪問(wèn)的頁(yè)面與權(quán)限對(duì)應(yīng),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-12-12
vue中axios的封裝問(wèn)題(簡(jiǎn)易版攔截,get,post)
這篇文章主要介紹了vue中axios的封裝問(wèn)題(簡(jiǎn)易版攔截,get,post),需要的朋友可以參考下2018-06-06
element表格el-table實(shí)現(xiàn)虛擬滾動(dòng)解決卡頓問(wèn)題
當(dāng)頁(yè)面數(shù)據(jù)過(guò)多,前端渲染大量的DOM時(shí),會(huì)造成頁(yè)面卡死問(wèn)題,本文主要介紹了element表格el-table實(shí)現(xiàn)虛擬滾動(dòng)解決卡頓問(wèn)題,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10
在Vue項(xiàng)目中使用d3.js的實(shí)例代碼
這篇文章主要介紹了在Vue項(xiàng)目中使用d3.js的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值價(jià)值,需要的朋友可以參考下2018-05-05
vue項(xiàng)目怎樣用nginx反向代理WebSocket請(qǐng)求地址
這篇文章主要介紹了vue項(xiàng)目怎樣用nginx反向代理WebSocket請(qǐng)求地址問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09

