微信小程序開發之上拉刷新
小程序的頂部和底部都有默認的監聽事件,這個組件的官方文檔scroll-view,這個組件的屬性如下:
scroll-view
可滾動視圖區域
屬性名 | 類型 | 默認值 | 說明 |
---|---|---|---|
scroll-x | Boolean | false | 允許橫向滾動 |
scroll-y | Boolean | false | 允許縱向滾動 |
upper-threshold | Number | 50 | 距頂部/左邊多遠時(單位px),觸發 scrolltoupper 事件 |
lower-threshold | Number | 50 | 距底部/右邊多遠時(單位px),觸發 scrolltolower 事件 |
scroll-top | Number | 設置豎向滾動條位置 | |
scroll-left | Number | 設置橫向滾動條位置 | |
scroll-into-view | String | 值應為某子元素id(id不能以數字開頭)。設置哪個方向可滾動,則在哪個方向滾動到該元素 | |
scroll-with-animation | Boolean | false | 在設置滾動條位置時使用動畫過渡 |
enable-back-to-top | Boolean | false | iOS點擊頂部狀態欄、安卓雙擊標題欄時,滾動條返回頂部,隻支持豎向 |
bindscrolltoupper | EventHandle | 滾動到頂部/左邊,會觸發 scrolltoupper 事件 | |
bindscrolltolower | EventHandle | 滾動到底部/右邊,會觸發 scrolltolower 事件 | |
bindscroll | EventHandle | 滾動時觸發,event.detail = {scrollLeft, scrollTop, scrollHeight, scrollWidth, deltaX, deltaY} |
其中scroll-y
,bindscrolltolower
,bindscrolltoupper
三個組件是分頁需要用到的。
實現程序
-
我們需要在*.wxml中聲明這個組件,組件內容加入需要展示的內容
<!-- - scroll-y="true" 為縱向滾動的必要聲明 - bindscrolltolower="loadMore" 為頁麵底部的監聽事件,即上拉刷新事件 - bindscrolltoupper="refresh" 為頁麵頂部的監聽事件,即下拉刷新事件 --> <scroll-view scroll-y="true" bindscrolltolower="loadMore" bindscrolltoupper="refresh"> <!--內容部分--> <view > <view wx:for="{{content}}" wx:key="item"> …… </view> </view> </scroll-view>
-
針對頂部和底部的監聽事件進行業務處理
//獲取應用實例 var app = getApp(); Page({ data: { userInfo: {}, content: [], winHeight: 0 }, page: {//分頁查詢參數 pageNum: 1,//初始頁碼 pageSize: 10,//單頁條數 dataSize: 0,//總數據條數,初始為0 hasRefresh: true //刷新狀態 }, onLoad: function () {//頁麵初始加載事件 var that = this;//複製this對象到臨時變量 wx.getSystemInfo({ success: function (res) { that.setData({ winHeight: res.windowHeight }); } }) loadData(that); }, loadMore: function () { wx.showToast({ title: '刷新中…', icon: 'loading', duration: 1000 }); var that = this; loadData(that); }, refresh: function () { console.log("下拉刷新....") this.onLoad(); } }) //數據加載 var loadData = function (that) { var pageCount = 1; if (that.page.dataSize > 0 && that.page.dataSize % that.page.pageSize == 0) { pageCount = parseInt(that.page.dataSize / that.page.pageSize); } else { pageCount = parseInt(that.page.dataSize / that.page.pageSize) + 1; } if (!that.page.hasRefresh) { return; } that.page.hasRefresh = false;//阻塞標識符,防止本次處理未結束前出現重複請求 var limit_start = that.page.pageSize * (that.page.pageNum - 1); var limit_end = that.page.pageSize * that.page.pageNum; if (that.page.pageNum > pageCount) { return; } wx.request({ url: 'https://*****/goods/list', data: { limit_start: limit_start, limit_end: limit_end }, method: 'GET', header: { 'content-type': 'application/json' }, success: function (res) { that.setData({ content: that.data.content.concat(res.data.list) }); that.page.dataSize = limit_start == 0 ? res.data.dataSize : that.page.dataSize; that.page.pageNum++; that.page.hasRefresh = true;//釋放阻塞 } }) }
注意事項
- 分頁數據拚裝到一頁進行顯示時,需要將每次請求的結果集進行合並處理,參考上文中
success: function (res) { that.setData({ 'content: that.data.content.concat(res.data.list)' }); }
- scroll-view標簽設置了bindscrolltolower,實現上拉加載時,數據重複請求
小程序的請求處理需要一定時間,而實際使用中,在請求結束前經常發生多次觸發底部監聽事件的情況,小程序組件中並為對此做處理,需要我們額外增加標識位來阻塞重複請求,參考樣例代碼中
hasRefresh
的處理
最後更新:2017-06-24 17:32:49