實現效果:假如有10條數據,分組展示,默認在當前頁面展示4個,點擊換一批,從第5個開始繼續展示,到最后一組,再重新返回到第一組
data() {
return {
qList: [],
//處理后的list
newList: [],
//截取第幾組的開始參數
timeStart: 0,
//截取第幾組的結束參數
timeEnd: 1,
//默認為0組
group: 0,
//一頁展示list數量
num: 3,
//點擊的次數
clickNum: 0
};
},
<div class="flex-align" @click="change">
<span>換一批</span>
</div>
// 換一批
change() {
if (this.qList.length > 4 && this.qList.length > this.num) {
//點擊的時候獲取分為幾組
this.listlen();
//每點擊一次記錄點擊次數
this.autoIncre();
this.clear();
this.renderR();
}
},
// 計算數據的長度,共分為幾組,如果不能整除則加1
listlen() {
var len = this.qList.length;
this.group = len / this.num;
if (len % this.num != 0) {
this.group = parseInt(this.group) + 1;
}
},
//計算將點擊次數和開始截取的參數清空, 如果點擊此時大于當前數據的組數,則重新開始計數。
clear() {
if (this.clickNum > this.group - 1) {
this.timeStart = 0;
this.timeEnd = 1;
this.clickNum = 0;
}
},
//每點擊一次,記錄次數
autoIncre() {
this.clickNum++;
this.timeStart++;
this.timeEnd++;
},
//截取當前每組的數據
renderR() {
this.newList = this.qList.slice(
this.num * this.timeStart,
this.num * this.timeEnd
);
console.log(this.newList);
}
},
//獲取總數據
created() {
let params = {
keyword: ""
};
this.$https("get", "/xxx/xxx", params)
.then(res => {
var data = res.data.data;
this.qList = data;
this.renderR();
})
.catch(err => {
});
}