element ui分頁多選,翻頁記憶的實例
更新時間:2019年09月03日 08:39:11 作者:夏天的胖子冬天的瘦子
今天小編就為大家分享一篇element ui分頁多選,翻頁記憶的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
先說需求:實時記錄當前選中的分頁中的列表,分頁保存數(shù)據(jù),然后在用選中的數(shù)據(jù)進行某些操作;PS:左下角的數(shù)字為記錄的當前選中的列表的和

直接上可用的代碼,前提已配置好各種環(huán)境
HTML部分
<!--table組件需要使用ref="table"-->
<template>
<div>
<el-table :data="tableData" ref="table" stripe style="width: 100%" @selection-change="handleSelectionChange">
<el-table-column type="selection" width="55"></el-table-column>
<el-table-column prop="name" label="商品名稱"></el-table-column>
<el-table-column prop="barcode" label="商品編碼"></el-table-column>
<el-table-column prop="quantity" label="區(qū)域總庫存"></el-table-column>
</el-table>
<div class="block" v-show="page.pageTotal>10">
<el-pagination @current-change="handleCurrentChange" :current-page.sync="page.currentPage" :page-size="page.pnum" layout="total, prev, pager, next , jumper" :total="page.pageTotal">
</el-pagination>
</div>
<div>
{{multipleSelectionAll.length}}
</div>
</div>
</template>
JS部分
export default {
data(){
return {
tableData: [], // 表格數(shù)據(jù)
multipleSelectionAll:[],//所有選中的數(shù)據(jù)包含跨頁數(shù)據(jù)
multipleSelection:[],// 當前頁選中的數(shù)據(jù)
idKey: 'barcode', // 標識列表數(shù)據(jù)中每一行的唯一鍵的名稱
page:{
//每頁數(shù)據(jù)量
pnum:10,
//數(shù)據(jù)總數(shù)
pageTotal:0,
//當前頁,從1開始
currentPage:1,
}
}
},
methods: {
handleCurrentChange(){
this.changePageCoreRecordData();
if(!this.$isNull(this.page.pageTotal)) this.getGoodsList();
},
handleSelectionChange (val) {
this.multipleSelection = val;
//實時記錄選中的數(shù)據(jù)
setTimeout(()=>{
this.changePageCoreRecordData();
}, 50)
},
setSelectRow() {
if (!this.multipleSelectionAll || this.multipleSelectionAll.length <= 0) {
return;
}
// 標識當前行的唯一鍵的名稱
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 () {
// 標識當前行的唯一鍵的名稱
let idKey = this.idKey;
let that = this;
// 如果總記憶中還沒有選擇的數(shù)據(jù),那么就直接取當前頁選中的數(shù)據(jù),不需要后面一系列計算
if (this.multipleSelectionAll.length <= 0) {
this.multipleSelectionAll = this.multipleSelection;
return;
}
// 總選擇里面的key集合
let selectAllIds = [];
this.multipleSelectionAll.forEach(row=>{
selectAllIds.push(row[idKey]);
})
let selectIds = []
// 獲取當前頁選中的id
this.multipleSelection.forEach(row=>{
selectIds.push(row[idKey]);
// 如果總選擇里面不包含當前頁選中的數(shù)據(jù),那么就加入到總選擇集合里
if (selectAllIds.indexOf(row[idKey]) < 0) {
that.multipleSelectionAll.push(row);
}
})
let noSelectIds = [];
// 得到當前頁沒有選中的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;
}
}
}
})
},
//請求接口部分
getGoodsList(){
let data = {};
data['page'] = this.page.currentPage;
data['pnum'] = this.page.pnum;
this.$ajax({
url:'goods/list',
data:data
}).then(val => {
this.tableData = val.data.rows ? val.data.rows : [];
this.page = {
pnum:10,
pageTotal:val.data.total,
currentPage:val.data.page,
};
setTimeout(()=>{
this.setSelectRow();
}, 50)
})
}
},
created () {
this.getGoodsList()
}
}
代碼可直接粘貼到項目中使用,親測可用,傻瓜式代碼
以上這篇element ui分頁多選,翻頁記憶的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
Bootstrap fileinput文件上傳預(yù)覽插件使用詳解
這篇文章主要為大家詳細介紹了Bootstrap fileinput文件上傳預(yù)覽插件的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
全面理解面向?qū)ο蟮?JavaScript(來自ibm)
要掌握好?JavaScript,首先一點是必須摒棄一些其他高級語言如?Java、C#?等類式面向?qū)ο笏季S的干擾,全面地從函數(shù)式語言的角度理解?JavaScript?原型式面向?qū)ο蟮奶攸c2013-11-11
淺談在fetch方法中添加header后遇到的預(yù)檢請求問題
下面小編就為大家?guī)硪黄獪\談在fetch方法中添加header后遇到的預(yù)檢請求問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08

