vue3實(shí)現(xiàn)表格編輯和刪除功能的示例代碼
實(shí)現(xiàn)效果如下

實(shí)現(xiàn)代碼
<template>
<div class="table">
<div class="tableTop">現(xiàn)有數(shù)據(jù)集</div>
<el-table
:data="filterTableData"
style="width: 100%"
class-name="elTable"
>
<template #empty>
<el-empty description="暫無(wú)數(shù)據(jù)" :image-size="200" />
</template>
<el-table-column label="模型創(chuàng)建日期" prop="date" />
<el-table-column label="模型名稱" prop="name" />
<el-table-column label="鏡像地址" prop="address" />
<el-table-column align="right">
<template #header>
<el-input
v-model="search"
size="small"
placeholder="請(qǐng)輸入您的模型名稱"
/>
</template>
<template #default="scope">
<el-button size="small" @click="handleEdit(scope.$index, scope.row)"
>編輯</el-button
>
<el-button
size="small"
type="danger"
@click="handleDelete(scope.$index)"
>刪除</el-button
>
</template>
</el-table-column>
</el-table>
<el-dialog v-model="dialogVisible">
<el-form :model="editingRowData">
<el-form-item label="模型名稱">
<el-input v-model="editingRowData.name" />
</el-form-item>
</el-form>
<div slot="footer">
<el-button @click="dialogVisible = false">取消</el-button>
<el-button type="primary" @click="handleUpdate">保存</el-button>
</div>
</el-dialog>
</div>
</template>
<script setup>
import {
ElInput,
ElTable,
ElTableColumn,
ElIcon,
ElButton,
ElEmpty,
ElMessageBox,
ElMessage,
ElDialog,
ElForm,
ElFormItem
} from 'element-plus'
import { computed, ref, reactive } from 'vue'
const search = ref('')
const editingRowData = ref({})
const dialogVisible = ref(false) // 編輯回顯時(shí)的彈窗
let editIndex = -1 // 表示沒(méi)有編輯的行
const tableData = reactive([
{
date: '2023-05-03',
name: 'Tom',
address: 'https://mirrors.aliyun.com/'
},
{
date: '2023-05-02',
name: 'John',
address: 'http://mirrors.163.com/'
},
{
date: '2023-05-04',
name: 'Morgan',
address: 'https://mirrors.hust.edu.cn/'
},
{
date: '2023-05-01',
name: 'Jessy',
address: 'https://npm.taobao.org/'
}
])
const handleEdit = (index, row) => {
editIndex = index
editingRowData.value = { ...row }
dialogVisible.value = true
}
const handleUpdate = () => {
if (editIndex !== -1) {
tableData.splice(editIndex, 1, { ...editingRowData })
dialogVisible.value = false
// tableData.value = editingRowData.value 直接賦值會(huì)失敗,因?yàn)橹苯淤x值只是賦值,而這里需要一個(gè)對(duì)象
tableData[editIndex] = Object.assign({}, editingRowData.value)
ElMessage({
type: 'success',
message: '修改成功'
})
} else {
ElMessage({
type: 'error',
message: '修改失敗!'
})
}
}
const filterTableData = computed(function () {
return tableData.filter(function (data) {
return (
!search.value ||
data.name.toLowerCase().includes(search.value.toLowerCase())
)
})
})
// 刪除數(shù)據(jù)集
const handleDelete = index => {
ElMessageBox.confirm('您確定要?jiǎng)h除該數(shù)據(jù)嗎?', '警告', {
confirmButtonText: '確定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
tableData.splice(index, 1)
ElMessage({
type: 'success',
message: '刪除成功'
})
})
}
</script>到此這篇關(guān)于vue3實(shí)現(xiàn)表格編輯和刪除功能的示例代碼的文章就介紹到這了,更多相關(guān)vue3表格編輯和刪除內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vuex新手進(jìn)階篇之a(chǎn)ctions的使用方法
actions用來(lái)處理mutations中的異步操作,觸發(fā)mutations中的函數(shù),下面這篇文章主要給大家介紹了關(guān)于vuex新手進(jìn)階篇之a(chǎn)ctions的使用方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10
vue+axios實(shí)現(xiàn)post文件下載
這篇文章主要為大家詳細(xì)介紹了vue+axios實(shí)現(xiàn)post文件下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09
淺析Vue為什么需要同時(shí)使用Ref和Reactive
這篇文章主要想來(lái)和大家一起探討一下Vue為什么需要同時(shí)使用Ref和Reactive,文中的示例代碼簡(jiǎn)潔易懂,感興趣的小伙伴可以跟隨小編一起了解一下2023-08-08
electron-vue?運(yùn)行報(bào)錯(cuò)?Object.fromEntries?is?not?a?function
Object.fromEntries()?是?ECMAScript?2019?新增的一個(gè)靜態(tài)方法,用于將鍵值對(duì)列表(如數(shù)組)轉(zhuǎn)換為對(duì)象,如果在當(dāng)前環(huán)境中不支持該方法,可以使用?polyfill?來(lái)提供類似功能,接下來(lái)通過(guò)本文介紹electron-vue?運(yùn)行報(bào)錯(cuò)?Object.fromEntries?is?not?a?function的解決方案2023-05-05
Vue子組件props從父組件接收數(shù)據(jù)并存入data
這篇文章主要介紹了Vue子組件props從父組件接收數(shù)據(jù)并存入data的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
vue實(shí)現(xiàn)接口封裝的實(shí)現(xiàn)示例
本文主要介紹了vue實(shí)現(xiàn)接口封裝的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
vue?如何綁定disabled屬性讓其不能被點(diǎn)擊
這篇文章主要介紹了vue?如何綁定disabled屬性讓其不能被點(diǎn)擊,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
element中el-table表頭通過(guò)header-row-style設(shè)置樣式
有些時(shí)候需要給element-ui表頭設(shè)置不同樣式,本文主要介紹了element中el-table表頭通過(guò)header-row-style設(shè)置樣式,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01
apache和nginx下vue頁(yè)面刷新404的解決方案
這篇文章主要介紹了apache和nginx下vue頁(yè)面刷新404的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12

