CRUD项目
main.js
import {createApp} from 'vue'
import './assets/style/reset.css'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'
const app = createApp(App)
app.use(ElementPlus)
app.mount('#app')
App.vue
<template>
<div class="table-box">
<h2 class="title">CRUD</h2>
<div class="query-box">
<el-input
class="el-inp"
v-model="inputQuery"
placeholder="请搜索姓名"
/>
<div>
<el-button
type="primary"
@click="handleAdd('add')">
增加
</el-button>
<el-button
type="danger"
@click="handleMultipleDelete()"
v-if="batchDeleteList.length > 0">
删除选中
</el-button>
</div>
</div>
<el-table
border
:data="tableData"
style="width: 100%"
@selection-change="handleSelectionChange"
ref="tableRef">
<el-table-column type="selection" width="55"/>
<el-table-column prop="name" label="姓名" width="120"/>
<el-table-column prop="phone" label="电话" width="120"/>
<el-table-column prop="email" label="邮箱" width="200"/>
<el-table-column prop="state" label="状态" width="120"/>
<el-table-column prop="address" label="地址" width="300"/>
<el-table-column fixed="right" label="操作" width="120" align="center">
<template #default="scope">
<el-button
link
type="primary"
style="color: #E47470"
size="small"
@click="handleDelete(scope.row)">
删除
</el-button>
<el-button
link
type="primary"
size="small"
@click="handleEdit('edit',scope.row)">
编辑
</el-button>
</template>
</el-table-column>
</el-table>
<el-dialog
v-model="dialogFormVisible"
draggable
width="40%"
>
<template #header>
{{ dialogTitle }}
</template>
<el-form :model="dialogForm">
<el-form-item label="姓名">
<el-input v-model="dialogForm.name" autocomplete="off"/>
</el-form-item>
<el-form-item label="电话">
<el-input v-model="dialogForm.phone" autocomplete="off"/>
</el-form-item>
<el-form-item label="邮箱">
<el-input v-model="dialogForm.email" autocomplete="off"/>
</el-form-item>
<el-form-item label="状态">
<el-input v-model="dialogForm.state" autocomplete="off"/>
</el-form-item>
<el-form-item label="地址">
<el-input v-model="dialogForm.address" autocomplete="off"/>
</el-form-item>
</el-form>
<template #footer>
<div class="dialog-footer">
<el-button @click="handleDialogCancel">取消</el-button>
<el-button type="primary" @click="handleDialogConfirm">确认</el-button>
</div>
</template>
</el-dialog>
</div>
</template>
<script setup>
import {ref, watch} from "vue";
let inputQuery = ref('')
let tableData = $ref([
{
id: 1,
name: 'Tom1',
state: 'California',
phone: '13800138000',
email: '13800138000@qq.com',
address: 'No. 189, Grove St, Los Angeles',
},
{
id: 2,
name: 'Tom2',
state: 'California',
phone: '13800138000',
email: '13800138000@qq.com',
address: 'No. 189, Grove St, Los Angeles',
},
{
id: 3,
name: 'Tom3',
state: 'California',
phone: '13800138000',
email: '13800138000@qq.com',
address: 'No. 189, Grove St, Los Angeles',
},
{
id: 4,
name: 'Tom4',
state: 'California',
phone: '13800138000',
email: '13800138000@qq.com',
address: 'No. 189, Grove St, Los Angeles',
},
])
let copyTableData = Object.assign(tableData)
let dialogFormVisible = $ref(false)
let dialogType = $ref('add')
let dialogTitle = $ref('新增')
let dialogForm = $ref({})
let batchDeleteList = $ref([])
const tableRef = ref()
watch(inputQuery, val => {
if (val.length > 0) {
tableData = tableData.filter(item => item.name.toLowerCase().match(val.toLowerCase()))
} else {
tableData = copyTableData
}
})
const handleSelectionChange = (row) => {
batchDeleteList = []
row.forEach(item => {
batchDeleteList.push(item.id)
})
}
const handleMultipleDelete = () => {
batchDeleteList.forEach(id => {
handleDelete({id})
})
batchDeleteList = []
tableRef.value.clearSelection()
}
const handleDelete = (row) => {
let index = tableData.findIndex(item => item.id === row.id)
tableData.splice(index, 1)
}
const handleEdit = (type, row) => {
dialogStateModify(type)
dialogForm = {...row}
}
const handleAdd = (type) => {
dialogStateModify(type)
dialogForm = {}
}
const dialogStateModify = (type) => {
dialogFormVisible = true
dialogType = type
if (dialogType === 'add') {
dialogTitle = '新增'
} else {
dialogTitle = '编辑'
}
}
const handleDialogCancel = () => {
dialogFormVisible = false
}
const handleDialogConfirm = () => {
dialogFormVisible = false
if (dialogType === 'add') {
tableData.push({
id: tableData.length + 1,
...dialogForm
})
} else if (dialogType === 'edit') {
let index = tableData.findIndex(item => dialogForm.id === item.id)
tableData[index] = dialogForm
}
}
</script>
<style>
.table-box {
width: 800px;
margin: 200px auto;
}
.title {
text-align: center;
}
.query-box {
margin-bottom: 20px;
display: flex;
justify-content: space-between;
align-items: center;
}
.el-inp {
width: 200px;
}
</style>