您好,登錄后才能下訂單哦!
這篇文章主要介紹關(guān)于element跨分頁(yè)操作選擇的案例分析,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
業(yè)務(wù)需求:在批量導(dǎo)出或者批量刪除的時(shí)候會(huì)涉及到跨分頁(yè)導(dǎo)出或者批量刪除,這是你會(huì)發(fā)現(xiàn),當(dāng)你選擇后點(diǎn)擊分頁(yè),發(fā)現(xiàn)之前選擇的數(shù)據(jù)已經(jīng)沒(méi)有了,現(xiàn)在就是要滿(mǎn)足分頁(yè)點(diǎn)擊分頁(yè)后原始數(shù)據(jù)保留
<template> <div> <el-table :data="tableData" tooltip-effect="dark" header-align="left" border ref="table" row-key="serviceDateId" @selection-change="handleSelectionChange" @row-dblclick="toDetail" @sort-change="sortChange" > <el-table-column type="selection" el-table-column width="50" fixed="left"></el-table-column> <el-table-column label="序號(hào)" width="80" fixed="left"> <template slot-scope="{row,$index}"> <span>{{$index + 1}}</span> </template> </el-table-column> <el-table-column label="服務(wù)日期" prop="serviceDate" sortable="custom" min-width="120" ></el-table-column> <el-table-column label="服務(wù)對(duì)象" prop="vsoName" min-width="120"></el-table-column> <el-table-column label="身份證號(hào)" prop="idCard" sortable="custom" min-width="200"></el-table-column> <el-table-column label="服務(wù)內(nèi)容" prop="serviceContentName" min-width="200"></el-table-column> <el-table-column label="服務(wù)結(jié)果" prop="serviceResultName" min-width="100"></el-table-column> <el-table-column label="志愿者" prop="volunteerName" sortable="custom" min-width="120" show-overflow-tooltip></el-table-column> <el-table-column label="志愿者所屬組織" prop="objName" min-width="200" show-overflow-tooltip></el-table-column> <el-table-column fixed="right" label="操作" width="150" header-align="center"> <template slot-scope="{row,$index}"> <span @click="handleEdit(row)" class="table-btn" v-has="{class: '編輯'}">編輯</span> <span @click="handleRemove($index, row)" class="table-btn" v-has="{class: '刪除'}">刪除</span> </template> </el-table-column> </el-table> <pagination v-show="total>0" :total="total" :page.sync="form.pageNum" :limit.sync="form.pageSize" @pagination="getData(form)" /> </div> </template> <script> export default { data(){ return{ ruleForm: { username: '', password:'' }, form: { pageNum: 1, // 分頁(yè)頁(yè)數(shù) pageSize: 10, // 分頁(yè)數(shù)量 }, multipleSelection: [], //多選的行數(shù)據(jù) multipleSelectionAll:[],// 當(dāng)前頁(yè)選中的數(shù)據(jù) idKey: 'idCard', } }, methods: { setSelectRow() { if (!this.multipleSelectionAll || this.multipleSelectionAll.length <= 0) { return; } // 標(biāo)識(shí)當(dāng)前行的唯一鍵的名稱(chēng) let idKey = this.idKey; let selectAllIds = []; let that = this; this.multipleSelectionAll.forEach(row=>{ selectAllIds.push(row[idKey]); }) this.$refs.table.clearSelection(); for(var i = 0; i < this.tableData.length; i++) { if (selectAllIds.indexOf(this.tableData[i][idKey]) >= 0) { // 設(shè)置選中,記住table組件需要使用ref="table" this.$refs.table.toggleRowSelection(this.tableData[i], true); } } }, // 記憶選擇核心方法 changePageCoreRecordData () { // 標(biāo)識(shí)當(dāng)前行的唯一鍵的名稱(chēng) let idKey = this.idKey; let that = this; //如果總記憶中還沒(méi)有選擇的數(shù)據(jù),那么就直接取當(dāng)前頁(yè)選中的數(shù)據(jù),不需要后面一系列計(jì)算 if (!this.multipleSelectionAll.length) { this.multipleSelectionAll = this.multipleSelection; return; } // 總選擇里面的key集合 let selectAllIds = []; this.multipleSelectionAll.forEach(row=>{ selectAllIds.push(row[idKey]); }) let selectIds = [] // 獲取當(dāng)前頁(yè)選中的id this.multipleSelection.forEach(row=>{ selectIds.push(row[idKey]); // 如果總選擇里面不包含當(dāng)前頁(yè)選中的數(shù)據(jù),那么就加入到總選擇集合里 if (selectAllIds.indexOf(row[idKey]) < 0) { that.multipleSelectionAll.push(row); } }) let noSelectIds = []; // 得到當(dāng)前頁(yè)沒(méi)有選中的id this.tableData.forEach(row=>{ if (selectIds.indexOf(row[idKey]) < 0) { noSelectIds.push(row[idKey]); } }) noSelectIds.forEach(id=>{ if (selectAllIds.indexOf(id) >= 0) { for(let i = 0; i< that.multipleSelectionAll.length; i ++) { if (that.multipleSelectionAll[i][idKey] == id) { // 如果總選擇中有未被選中的,那么就刪除這條 that.multipleSelectionAll.splice(i, 1); break; } } } }) }, // 多選的行數(shù)據(jù) handleSelectionChange(val) { this.multipleSelection = val setTimeout(()=>{ this.changePageCoreRecordData(); }, 50) }, // 獲取表格數(shù)據(jù) getData(form) { let parmas = _.cloneDeep(form); parmas.liveArea = typeof(parmas.liveArea) === 'object'?parmas.liveArea.join(''):parmas.liveArea; recordSearch(form).then(res => { if (res.rows) { this.tableData = res.rows; this.total = res.total; this.exportData = _.cloneDeep(form); setTimeout(()=>{ this.setSelectRow(); }, 50) } else { this.tableData = []; this.total = 0; } }) } }, mounted(){ this.getData(this.form) } } </script> <style lang="sass" scoped> </style>
以上是關(guān)于element跨分頁(yè)操作選擇的案例分析的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。