elementUI多選框反選的實(shí)現(xiàn)代碼
最近有一個需求,點(diǎn)擊添加按鈕,彈出窗口(窗口顯示多選、可翻頁、可檢索列表),選中多條信息,當(dāng)我點(diǎn)擊確定按鈕,把選中信息顯示在頁面上;點(diǎn)擊取消,選中信息不顯示在頁面上。再次打開,把在頁面上的信息顯示選中狀態(tài)。
思路:一開始選用elementUI官網(wǎng)例子,使用selection-change,但是它只顯示當(dāng)前改變的選擇,不能滿足我翻頁及檢索后還能選中數(shù)據(jù)的問題
toggleSelection(rows) {
if (rows) {
rows.forEach(row => {
this.$refs.multipleTable.toggleRowSelection(row);
});
} else {
this.$refs.multipleTable.clearSelection();
}
}
后來查詢api,發(fā)現(xiàn)了另外2個api,頁面上的存在本地字段 glht,列表上選中的存在本地字段 yglht.
每次要計算頁面顯示的數(shù)據(jù)是列表的第幾條數(shù)據(jù),直接把對象放里面并不會勾選我上傳選中的數(shù)據(jù)。
emmm....知道有點(diǎn)蠢,但是我還沒想到別的辦法...
彈框:
<el-dialog class="contract_modal" title="信息" :visible.sync="glht_modal" width="80%" :modal="false" @close="cancel">
<el-form :inline="true" :model="searchData" label-width="90px">
<el-form-item label="名稱:">
<el-input v-model.trim="searchData.mc_" size="small" class="contract_input"></el-input>
</el-form-item>
<span class="list_btns">
<el-button type="primary" size="small" @click="listSearch(page, 1)" class="con_btn">搜索</el-button>
<el-button size="small" @click="searchData = {}" class="con_btn">清空</el-button>
</span>
</el-form>
<div id="list_body" class="list-body" style="padding-left: 0;">
<el-table :data="tableData" stripe border style="width: 100%" max-height="480" ref="multipleTable" @select-all="handleSelectionAll" @select="handleSelectionChange">
<el-table-column type="selection" width="26" align="right"></el-table-column>
<el-table-column type="index" width="50" label="序號" align="left" header-align="left"></el-table-column>
<el-table-column prop="mc_" label="名稱" width="180" show-overflow-tooltip align="left"></el-table-column>
</el-table>
<div class="list-pagination">
<el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
:current-page.sync="page.page" :page-sizes="[10, 20, 50, 100]":page-size="page.pageCount"
layout="total, sizes, prev, pager, next, jumper, ->"
:total="page.total"></el-pagination>
</div>
</div>
<div slot="footer" class="dialog-footer">
<el-button type="primary" @click="save" size="small">確定</el-button>
<el-button @click="cancel" size="small">取消</el-button>
</div>
</el-dialog>
methods 里,全選時與選中單個時所做的操作:
// 全選 當(dāng)val有數(shù)據(jù),即為全選;為空數(shù)組,即為取消全選
handleSelectionAll (val) {
// 獲取所有選中的數(shù)據(jù)
this.records = JSON.parse(localStorage.getItem('glht'))
if(val.length !== 0) {
//全選
// this.records = Array.from(new Set(val.concat(this.records))) 發(fā)現(xiàn)去重不好用!只能手動push了
// 全選選中的一定是本頁所有數(shù)據(jù),所以循環(huán)本頁
this.tableData.forEach((v) => {
if(this.records.find((e,index) => { return v.id_ === e.id_})){}else {
// 如果數(shù)組中沒有就把選中的push進(jìn)去
this.records.push(v)
}
})
} else {
// 取消全選,在選中的所有信息中把本頁列表有的刪除
this.tableData.forEach((v) => {
this.records.forEach((e,index) => {
if (e.id_ === v.id_) {
this.records.splice(index, 1)
}
})
})
}
// 每次選的數(shù)據(jù)暫時存一下
localStorage.setItem('glht', JSON.stringify(this.records))
},
// 單選
handleSelectionChange(val, row) {
// 獲取所有選中的數(shù)據(jù)
this.records = JSON.parse(localStorage.getItem('glht'))
if (this.records.length === 0) {
// 還沒有選中任何數(shù)據(jù)
this.records = [row]
} else {
if (this.records.filter(i => { return i.id_ === row.id_ }).length === 0) {
// 已選中的數(shù)據(jù)里沒有本條(取消),把這條加進(jìn)來
this.records.push(row)
} else {
// 已選中的數(shù)據(jù)里有本條(取消選中),把這條刪除
this.records.forEach((e,index) => {
if (e.id_ === row.id_) {
this.records.splice(index, 1)
}
})
}
}
// 每次選的數(shù)據(jù)暫時存一下
localStorage.setItem('glht', JSON.stringify(this.records))
},
methods 里,獲取彈出框列表與選中數(shù)據(jù):
listGet() {
this.$axios.post("interface", {}, { params: searchData }).then(res => {
if (res.data.success) {
this.tableData = res.data.data.rows;
this.page.total = res.data.data.total;
this.records = JSON.parse(localStorage.getItem('yglht'))
this.toggleSelection(JSON.parse(localStorage.getItem('yglht'))); // 反選操作
} else {
this.$message.error(res.data.message)
}
})
.catch(err => console.log(err));
},
// 反選處理
toggleSelection(rows) {
let arr =[]
this.tableData.forEach((e, index) => {
rows.forEach((i, ind) => {
if (e.id_ === i.id_) {
arr.push(this.tableData[index])
}
})
})
if (arr) {
this.$nextTick(() => {
arr.forEach(row => {
this.$refs.multipleTable.toggleRowSelection(row);
});
})
} else {
this.$refs.multipleTable.clearSelection();
}
},
methods 里,保存與取消單個時所做的操作:
save () {
this.glht_modal = false
localStorage.setItem('yglht', localStorage.getItem('glht'))
this.yglht()
},
cancel () {
this.glht_modal = false
// 取消時 取在頁面上的值
localStorage.setItem('glht', localStorage.getItem('yglht'))
// this.toggleSelection(JSON.parse(localStorage.getItem('yglht'))); 直接寫不好用
this.listGet({}) // 獲取彈出框列表數(shù)據(jù),這里調(diào)用一次是因?yàn)榉乐乖俅未蜷_彈出框閃現(xiàn)之前選擇的內(nèi)容
this.yglht()
},
yglht() {
this.list = JSON.parse(localStorage.getItem('yglht'))
// 處理this.list中地址、時間等頁面顯示問題
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue項(xiàng)目之?dāng)?shù)量占比進(jìn)度條實(shí)現(xiàn)方式
這篇文章主要介紹了vue項(xiàng)目之?dāng)?shù)量占比進(jìn)度條實(shí)現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
vue 彈窗時 監(jiān)聽手機(jī)返回鍵關(guān)閉彈窗功能(頁面不跳轉(zhuǎn))
這篇文章主要介紹了vue 彈窗時 監(jiān)聽手機(jī)返回鍵關(guān)閉彈窗功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值(頁面不跳轉(zhuǎn)) ,需要的朋友可以參考下2019-05-05
vue如何將html內(nèi)容轉(zhuǎn)為圖片并下載到本地
這篇文章主要介紹了vue如何將html內(nèi)容轉(zhuǎn)為圖片并下載到本地,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01

