elementPlus組件之表格編輯并校驗功能實現(xiàn)
更新時間:2024年12月05日 10:36:36 作者:碼嘍的自我修養(yǎng)
本文詳細介紹了如何使用Element Plus組件實現(xiàn)表格的單條數(shù)據(jù)新增、編輯、刪除操作,并對數(shù)據(jù)進行校驗,代碼簡單易懂,感興趣的朋友跟隨小編一起看看吧
?功能描述
在表格中可以進行單條數(shù)據(jù)的新增、編輯、刪除操作,并能對數(shù)據(jù)進行校驗。具體如下圖

功能解析
- 表格和表單切換,表單嵌套表格,通過
v-if判斷當前行的數(shù)據(jù)是否切換為表單組件。 - 表格編輯定位,通過自帶的
scope.$index做定位具體是哪條數(shù)據(jù)做編輯操作,并將該條數(shù)據(jù)解構賦值給form - 取消后數(shù)據(jù)還原,因為form數(shù)據(jù)是解構后的,直接取消編輯狀態(tài)即可。如果是新增狀態(tài)下的取消,則需要刪除最后一條數(shù)據(jù)。
- 新增數(shù)據(jù),初始化form數(shù)據(jù),并push到表格數(shù)據(jù)
功能實現(xiàn)
<template>
<el-form ref="tableFormRef" :model="table.form" :rules="table.rules" status-icon>
<el-table :data="table.data" border stripe>
<el-table-column type="index" width="50" label="序號" />
<el-table-column prop="min" label="下限值(kwh)">
<template #default="scope">
<el-form-item v-if="scope.$index === table.idx" prop="min">
<el-input v-model.number="table.form.min" placeholder="請輸入下限值" />
</el-form-item>
<span v-else>{{ scope.row.min }}</span>
</template>
</el-table-column>
<el-table-column prop="max" label="上限值(kwh)">
<template #default="scope">
<el-form-item v-if="scope.$index === table.idx" prop="max">
<el-input v-model.number="table.form.max" placeholder="請輸入上限值" />
</el-form-item>
<span v-else>{{ scope.row.max }}</span>
</template>
</el-table-column>
<el-table-column prop="price" label="單價(元)">
<template #default="scope">
<el-form-item v-if="scope.$index === table.idx" prop="price">
<el-input v-model.number="table.form.price" placeholder="請輸入單價" />
</el-form-item>
<span v-else>{{ scope.row.price }}</span>
</template>
</el-table-column>
<el-table-column label="操作">
<template #default="scope">
<template v-if="scope.$index === table.idx">
<el-button @click="saveItem(scope.$index)">保存</el-button>
<el-button @click="cancelItem(scope.$index)">取消</el-button>
</template>
<template v-else>
<el-button @click="editItem(scope.$index, scope.row)">編輯</el-button>
<el-button @click="deleteItem(scope.$index)">刪除</el-button>
</template>
</template>
</el-table-column>
</el-table>
<div class="add-table-item" @click="addItem">新增</div>
</el-form>
</template>
<script setup>
// 校驗規(guī)則最小數(shù)
const rangeNumberMin = (rule, value, callback) => {
console.log(value >= table.form.max, value, table.form.max)
if (value < 0) {
callback(new Error('數(shù)字不能為負數(shù)'))
} else if (value >= table.form.max) {
callback(new Error('下限值不能大于或等于上限值'))
} else {
callback()
}
}
// 校驗規(guī)則最大數(shù)
const rangeNumberMax = (rule, value, callback) => {
if (value < 0) {
callback(new Error('數(shù)字不能為負數(shù)'))
} else if (value <= table.form.min) {
callback(new Error('上限值不能小于或等于下限值'))
} else {
callback()
}
}
// 表格表單數(shù)據(jù)
const table = reactive({
data: [ // 表格數(shù)據(jù)
{ price: 100, max: 100, min: 0 },
{ price: 100, max: 200, min: 0 },
{ price: 100, max: 100, min: 10 },
{ price: 100, max: 200, min: 30 },
{ price: 100, max: 100, min: 50 }
],
idx: -1, // 編輯第幾條數(shù)據(jù)(-1則為不編輯)
isAdd: false, // 是否為新增數(shù)據(jù),如果為新增,取消時則需要做刪除操作
form: { // 表單數(shù)據(jù),單獨處理是為了方便取消操作和規(guī)則校驗
price: '',
max: '',
min: ''
},
rules: { // 規(guī)則校驗
min: [
{ required: true, message: '請輸入下限值', trigger: 'blur' },
{ validator: rangeNumberMin, trigger: 'blur' }
],
max: [
{ required: true, message: '請輸入上限值', trigger: 'blur' },
{ validator: rangeNumberMax, trigger: 'blur' }
],
price: [{ required: true, message: '請輸入價格限值', trigger: 'blur' }]
}
})
// 新增
const addItem = () => {
if (table.idx > -1) {
return
}
table.isAdd = true // 標記,取消后會刪除編輯數(shù)據(jù)
table.idx = table.data.length // 編輯最新條數(shù)
table.data.push(table.form)
}
// 編輯
const editItem = (idx, val) => {
if (table.idx > -1) {
return
}
table.isAdd = false // 防止取消后刪除編輯數(shù)據(jù)
table.idx = idx // 編輯當前
table.form = { ...val } // 將需要編輯的數(shù)據(jù)解構賦值給表單,此處是做了深拷貝,如果數(shù)據(jù)復雜,需要另外做深拷貝處理
}
// 取消
const cancelItem = idx => {
// 如果是新增狀態(tài)下,則刪除編輯數(shù)據(jù)
if (table.isAdd) {
table.data.splice(idx, 1)
}
// 初始化數(shù)據(jù)
table.form = {
price: '',
max: '',
min: ''
}
table.idx = -1
table.isAdd = false
}
// 刪除
const deleteItem = idx => {
if (table.idx > -1) {
return
}
table.data.splice(idx, 1)
}
const tableFormRef = ref()
const saveItem = idx => {
tableFormRef.value.validate(valid => {
if (valid) {
// 替換插入數(shù)據(jù),如數(shù)據(jù)解構復雜,請自行做深拷貝操作
table.data.splice(idx, 1, { ...table.form })
// 初始化數(shù)據(jù)
table.form = {
price: '',
max: '',
min: ''
}
table.idx = -1
table.isAdd = false
} else {
console.log('error submit!')
return false
}
})
}
</script>
<style lang="scss" scoped>
.add-table-item {
width: 100%;
height: 49px;
border-width: 0px 1px 1px 1px;
border-style: solid;
border-color: var(--el-border-color-lighter);
text-align: center;
line-height: 49px;
background-color: var(--el-fill-color-lighter);
cursor: pointer;
}
</style>
到此這篇關于elementPlus組件之表格編輯并校驗的文章就介紹到這了,更多相關elementPlus表格編輯并校驗內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Vue2(三)實現(xiàn)子菜單展開收縮,帶動畫效果實現(xiàn)方法
這篇文章主要介紹了vue實現(xiàn)收縮展開效果的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-04-04
vue-devtools?開發(fā)工具插件之支持vue3?chrome?瀏覽器插件
這篇文章主要介紹了vue-devtools?開發(fā)工具插件之支持vue3?chrome?瀏覽器插件,用這個版本主要是為了支持vue3?推薦直接下載,文中給大家提供了下載地址,感興趣的朋友跟隨小編一起看看吧2022-01-01
element plus el-table多選框跨頁多選保留功能
這篇文章主要介紹了element plus el-table多選框跨頁多選保留功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2025-05-05
Vue MVVM模型與data及methods屬性超詳細講解
MVVM旨在利用WPF中的數(shù)據(jù)綁定函數(shù),通過從視圖層中幾乎刪除所有GUI代碼(代碼隱藏),更好地促進視圖層開發(fā)與模式其余部分的分離,這篇文章主要介紹了Vue MVVM模型與data及methods屬性2022-10-10
詳解如何在Vue Router中實現(xiàn)動態(tài)路由匹配
在構建復雜的單頁面應用程序(SPA)時,動態(tài)路由匹配是一項強大的功能,它允許我們根據(jù)URL中的參數(shù)動態(tài)加載內容,Vue Router 提供了動態(tài)路由匹配的能力,使得我們可以根據(jù)不同的URL參數(shù)展示不同的組件或數(shù)據(jù),本文將介紹如何在Vue Router中實現(xiàn)動態(tài)路由匹配2025-05-05

