先在data里定義當前頁,每頁條數,全部總條數 在wxml頁面遍歷cartListDatas
data: {
page: 0, //當前頁
pages: 10, //每頁條數
total: 0, //q全部總條數
cartListDatas: [],
},
請求介面資料
// 獲取資料
getCartList(isMerge) {
var deptId = wx.getStorageSync('deptId')
var pages = this.data.pages
var page = this.data.page
config.reqestApi(app.globalData.api + `/cart/getCars?deptId=${deptId}&page=${page}&size=${pages}`).then(res => {
console.log("cart=============", res)
if (res.data.code == 200) {
let cartListDatas = this.data.cartListDatas
if (!isMerge) {
//不合并,shop需要初始化
cartListDatas = [];
}
cartListDatas = cartListDatas.concat(res.data.data.content) || [];
this.setData({
cartListDatas,
total: res.data.data.totalElements,
})
}
})
},
設定分頁
// 分頁
setPage() {
const {
page,
pages,
total
} = this.data;
// const totalPages = getTotalPages(total, pages);
const remainder = Number(total) % Number(pages);
const value = Math.floor(total / pages);
const totalPages = remainder == 0 ? value - 1 : Number(value);
// return totalPages
console.log("totalPages======", totalPages);
if (totalPages > 0) {
this.data.page += 1
}
if (page >= totalPages || this.isLoading) {
//控制觸底是否加載需要兩個條件,滿足一下兩個條件,都不能呼叫請求函式
// 1.當前頁是最后一頁,
// 2. 正在加載中
return
}
//分頁加載需要傳遞isMerge=true引數,表示需要合并到原來的陣列上
this.getCartList(true)
},
頁面上拉事件處理函式
/**
* 頁面上拉觸底事件的處理函式
*/
onReachBottom: function () {
this.setPage()
},
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/225383.html
標籤:其他
上一篇:Android 線性布局
