vue前端實(shí)現(xiàn)表格數(shù)據(jù)增查改刪功能
一、添加(增)-unshift首插入
1、【新增按鈕】添加點(diǎn)擊事件cilck;
<el-button @click="handleAdd()">添加</el-button>
2、點(diǎn)擊【新增按鈕】:
2.1、打開彈框;
2.2、內(nèi)容為空。
handleAdd() { this.dialogVisible = true this.addForm = { name: '', number: '', score: '', sex: '' } },
3、彈框【確定】:
3.1、動(dòng)態(tài)數(shù)據(jù)表格插入新增數(shù)據(jù);
3.2、全部數(shù)據(jù)表格插入新增數(shù)據(jù);
3.3、關(guān)閉彈框。
handleOk() { this.tableData.unshift(this.addForm) this.allData.unshift(this.addForm) this.dialogVisible = false }
二、搜索(查)-filter過濾
1、【查詢】按鈕添加點(diǎn)擊事件cilck;
<el-button type="primary" @click="handleSelect()">查詢</el-button>
2、點(diǎn)擊【查詢】:
2.1、姓名查詢:
handleSelect() { this.tableData = this.allData.filter(item => { if (item.name.includes(this.formInline.name)) { return true } }) }
2.2、學(xué)號(hào)查詢:
handleSelect() { this.tableData = this.allData.filter(item => { if (item.number === this.formInline.number) { return true } }) }
2.3、姓名+學(xué)號(hào)查詢:
handleSelect() { //姓名+學(xué)號(hào)同時(shí)為空 if (this.formInline.name === '' && this.formInline.number === '') { this.tableData = [...this.allData] } else if (this.formInline.name !== '' && this.formInline.number === '') { //姓名查詢,學(xué)號(hào)為空 this.tableData = this.allData.filter(item => { if (item.name.includes(this.formInline.name)) { return true } }) } else if (this.formInline.name === '' && this.formInline.number !== '') { //學(xué)號(hào)查詢,姓名為空 this.tableData = this.allData.filter(item => { if (item.number === this.formInline.number) { return true } }) } else if (this.formInline.name !== '' && this.formInline.number !== '') { //姓名+學(xué)號(hào)查詢,都不為空 this.tableData = this.allData.filter(item => { if (item.name.includes(this.formInline.name) && item.number === this.formInline.number) { return true } }) } }
三、編輯(改)-splice替換
1、【編輯】按鈕綁定點(diǎn)擊事件;
當(dāng)前行獲?。╯cope)。
<el-button type="success" plain size="small" @click="handleEdit(scope)">編輯</el-button>
2、點(diǎn)擊【編輯】:
2.1、判斷為非添加(編輯)狀態(tài);
2.1.1、彈框標(biāo)題為【編輯】;
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、解構(gòu)函數(shù):{...scope.row};為了后面獲取對象的index;
2.3、打開彈框。
handleEdit(scope) { this.isView = false this.isAdd = false this.tkTitle = '編輯' this.addForm = { ...scope.row } this.dialogVisible = true },
3、點(diǎn)擊【確定】:
3.1、判斷彈框狀態(tài)是【添加】or【編輯】;
3.2、獲取index;
3.3、找到表格index的一條,替換成修改后的當(dāng)前彈框數(shù)據(jù)。
4、關(guān)閉彈框。
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、【刪除】按鈕綁定點(diǎn)擊事件;
<el-button type="warning" plain size="small" @click="handleDelete(scope)">刪除</el-button>
2、點(diǎn)擊【刪除】:
2.1、找到當(dāng)前行的index;
2.2、刪除當(dāng)前index對應(yīng)的數(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、【重置】添加點(diǎn)擊事件cilck;
<el-button @click="handleReset()">重置</el-button>
2、點(diǎn)擊【重置】:
2.1、查詢條件為空;
2.2、表格內(nèi)容顯示全部:運(yùn)用解構(gòu)函數(shù),allData數(shù)組淺拷貝給tableData數(shù)組。
handleReset() { this.formInline = { name: '', number: '', sex: '' } this.tableData = [...this.allData] }
六、查看
1、【查看】綁定點(diǎn)擊事件click;
顯示表格時(shí),當(dāng)前行數(shù)據(jù)的獲取:slot-scope="scope"
<template slot-scope="scope"> <el-button type="primary" plain size="small" @click="handleView(scope)">查看</el-button> </template>
2、點(diǎn)擊【查看】:
2.1、彈框是“查看”狀態(tài);
2.1.1、彈框標(biāo)題顯示為“查看”;
2.1.2、查看狀態(tài)下,內(nèi)容不可編輯;
2.2、彈框顯示當(dāng)前行數(shù)據(jù);
2.3、打開彈框。
:title="tkTitle" :disabled="isView"
handleView(scope) { this.isView = true this.tkTitle = '查看' this.addForm = scope.row this.dialogVisible = true }
總結(jié)
到此這篇關(guān)于vue前端實(shí)現(xiàn)表格數(shù)據(jù)增查改刪功能的文章就介紹到這了,更多相關(guān)vue表格數(shù)據(jù)增查改刪內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue+elementUl導(dǎo)入文件方式(判斷文件格式)
這篇文章主要介紹了vue+elementUl導(dǎo)入文件方式(判斷文件格式),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08詳解Vue-cli中的靜態(tài)資源管理(src/assets和static/的區(qū)別)
這篇文章主要介紹了Vue-cli中的靜態(tài)資源管理(src/assets和static/的區(qū)別,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06淺談vue中resetFields()使用注意事項(xiàng)
這篇文章主要介紹了淺談vue中resetFields()使用注意事項(xiàng),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08vite打包出現(xiàn)?"default"?is?not?exported?by?"
這篇文章主要給大家介紹了關(guān)于vite打包出現(xiàn)?"default"?is?not?exported?by?"node_modules/...問題的解決辦法,文中通過代碼將解決的辦法介紹的非常詳細(xì),對同樣遇到這個(gè)問題的朋友具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-06-06vue.js實(shí)現(xiàn)簡單計(jì)時(shí)器功能
這篇文章主要為大家詳細(xì)介紹了vue.js實(shí)現(xiàn)簡單計(jì)時(shí)器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08