vue3實現(xiàn)表格編輯和刪除功能的示例代碼
更新時間:2024年01月10日 10:15:56 作者:huoyueyi
這篇文章主要為大家詳細介紹了vue3實現(xiàn)表格編輯和刪除功能的相關(guān)知識,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
實現(xiàn)效果如下

實現(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="暫無數(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="請輸入您的模型名稱"
/>
</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) // 編輯回顯時的彈窗
let editIndex = -1 // 表示沒有編輯的行
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 直接賦值會失敗,因為直接賦值只是賦值,而這里需要一個對象
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('您確定要刪除該數(shù)據(jù)嗎?', '警告', {
confirmButtonText: '確定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
tableData.splice(index, 1)
ElMessage({
type: 'success',
message: '刪除成功'
})
})
}
</script>到此這篇關(guān)于vue3實現(xiàn)表格編輯和刪除功能的示例代碼的文章就介紹到這了,更多相關(guān)vue3表格編輯和刪除內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue項目中使用jsonp抓取跨域數(shù)據(jù)的方法
這篇文章主要介紹了Vue項目中使用jsonp抓取跨域數(shù)據(jù)的方法,本文通過實例代碼講解的非常詳細,需要的朋友可以參考下2019-11-11
你不知道的Vue技巧之--開發(fā)一個可以通過方法調(diào)用的組件(推薦)
這篇文章主要介紹了你不知道的Vue技巧之--開發(fā)一個可以通過方法調(diào)用的組件,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-04-04

