vue前端實現(xiàn)表格數(shù)據(jù)增查改刪功能

一、添加(增)-unshift首插入
1、【新增按鈕】添加點擊事件cilck;
<el-button @click="handleAdd()">添加</el-button>
2、點擊【新增按鈕】:
2.1、打開彈框;
2.2、內容為空。
handleAdd() {
this.dialogVisible = true
this.addForm = {
name: '',
number: '',
score: '',
sex: ''
}
},
3、彈框【確定】:
3.1、動態(tài)數(shù)據(jù)表格插入新增數(shù)據(jù);
3.2、全部數(shù)據(jù)表格插入新增數(shù)據(jù);
3.3、關閉彈框。
handleOk() {
this.tableData.unshift(this.addForm)
this.allData.unshift(this.addForm)
this.dialogVisible = false
}二、搜索(查)-filter過濾
1、【查詢】按鈕添加點擊事件cilck;
<el-button type="primary" @click="handleSelect()">查詢</el-button>
2、點擊【查詢】:
2.1、姓名查詢:
handleSelect() {
this.tableData = this.allData.filter(item => {
if (item.name.includes(this.formInline.name)) {
return true
}
})
}2.2、學號查詢:
handleSelect() {
this.tableData = this.allData.filter(item => {
if (item.number === this.formInline.number) {
return true
}
})
}2.3、姓名+學號查詢:
handleSelect() {
//姓名+學號同時為空
if (this.formInline.name === '' && this.formInline.number === '') {
this.tableData = [...this.allData]
} else if (this.formInline.name !== '' && this.formInline.number === '') {
//姓名查詢,學號為空
this.tableData = this.allData.filter(item => {
if (item.name.includes(this.formInline.name)) {
return true
}
})
} else if (this.formInline.name === '' && this.formInline.number !== '') {
//學號查詢,姓名為空
this.tableData = this.allData.filter(item => {
if (item.number === this.formInline.number) {
return true
}
})
} else if (this.formInline.name !== '' && this.formInline.number !== '') {
//姓名+學號查詢,都不為空
this.tableData = this.allData.filter(item => {
if (item.name.includes(this.formInline.name) && item.number === this.formInline.number) {
return true
}
})
}
}三、編輯(改)-splice替換
1、【編輯】按鈕綁定點擊事件;
當前行獲?。╯cope)。
<el-button type="success" plain size="small" @click="handleEdit(scope)">編輯</el-button>
2、點擊【編輯】:
2.1、判斷為非添加(編輯)狀態(tài);
2.1.1、彈框標題為【編輯】;
2.1.2、編輯狀態(tài)姓名不可編輯;
<el-form-item label="姓名">
<el-input v-model="addForm.name" :disabled="isView || !isAdd"></el-input>
</el-form-item>2.2、解構函數(shù):{...scope.row};為了后面獲取對象的index;
2.3、打開彈框。
handleEdit(scope) {
this.isView = false
this.isAdd = false
this.tkTitle = '編輯'
this.addForm = { ...scope.row }
this.dialogVisible = true
},3、點擊【確定】:
3.1、判斷彈框狀態(tài)是【添加】or【編輯】;
3.2、獲取index;
3.3、找到表格index的一條,替換成修改后的當前彈框數(shù)據(jù)。
4、關閉彈框。
handleOk() {
//添加確定
if (this.isAdd) {
this.tableData.unshift(this.addForm)
this.allData.unshift(this.addForm)
this.dialogVisible = false
} else {
//編輯確定
const index = this.tableData.findIndex(item => {
return item.name = this.addForm.name
})
if (index !== -1) {
this.tableData.splice(index, 1, this.addForm)
}
this.dialogVisible = false
this.allData = [...this.tabledata]
}四、刪除(刪)-splice刪除
1、【刪除】按鈕綁定點擊事件;
<el-button type="warning" plain size="small" @click="handleDelete(scope)">刪除</el-button>
2、點擊【刪除】:
2.1、找到當前行的index;
2.2、刪除當前index對應的數(shù)據(jù)。
handleDelete(scope) {
const index = this.tableData.findIndex(item => {
return item.name === scope.row.name
})
if (index !== -1) {
this.tableData.splice(index, 1)
this.allData = [...this.tableData]
}
}五、重置
1、【重置】添加點擊事件cilck;
<el-button @click="handleReset()">重置</el-button>
2、點擊【重置】:
2.1、查詢條件為空;
2.2、表格內容顯示全部:運用解構函數(shù),allData數(shù)組淺拷貝給tableData數(shù)組。
handleReset() {
this.formInline = {
name: '',
number: '',
sex: ''
}
this.tableData = [...this.allData]
}六、查看
1、【查看】綁定點擊事件click;
顯示表格時,當前行數(shù)據(jù)的獲?。簊lot-scope="scope"
<template slot-scope="scope">
<el-button type="primary" plain size="small" @click="handleView(scope)">查看</el-button>
</template>
2、點擊【查看】:
2.1、彈框是“查看”狀態(tài);
2.1.1、彈框標題顯示為“查看”;
2.1.2、查看狀態(tài)下,內容不可編輯;
2.2、彈框顯示當前行數(shù)據(jù);
2.3、打開彈框。
:title="tkTitle" :disabled="isView"
handleView(scope) {
this.isView = true
this.tkTitle = '查看'
this.addForm = scope.row
this.dialogVisible = true
}總結
到此這篇關于vue前端實現(xiàn)表格數(shù)據(jù)增查改刪功能的文章就介紹到這了,更多相關vue表格數(shù)據(jù)增查改刪內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解Vue-cli中的靜態(tài)資源管理(src/assets和static/的區(qū)別)
這篇文章主要介紹了Vue-cli中的靜態(tài)資源管理(src/assets和static/的區(qū)別,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06
vite打包出現(xiàn)?"default"?is?not?exported?by?"
這篇文章主要給大家介紹了關于vite打包出現(xiàn)?"default"?is?not?exported?by?"node_modules/...問題的解決辦法,文中通過代碼將解決的辦法介紹的非常詳細,對同樣遇到這個問題的朋友具有一定的參考借鑒價值,需要的朋友可以參考下2024-06-06

