微信小程序?qū)崿F(xiàn)上拉加載功能示例【加載更多數(shù)據(jù)/觸底加載/點擊加載更多數(shù)據(jù)】
本文實例講述了微信小程序?qū)崿F(xiàn)上拉加載功能。分享給大家供大家參考,具體如下:
開發(fā)需求
進入頁面,加載初始數(shù)據(jù),當向上拖動頁面至底部,自動加載新的數(shù)據(jù),即上拉加載更多數(shù)據(jù)。

演示

index.wxml
<!-- 數(shù)據(jù)列表 -->
<view wx:for="{{listdata}}" wx:key="listdata" class='listitem'>
<view class='title'>{{item.title}}</view>
<view class='title'>資源ID:{{item.id}}</view>
<image src="{{item.coverimg}}" class='cover'></image>
</view>
<!-- 如果還有更多數(shù)據(jù)可以加載,則顯示玩命加載中 -->
<view class="load-more-wrap">
<block wx:if="{{hasMore}}">
<view class="load-content">
<text class="weui-loading"/><text class="loading-text">玩命加載中</text>
</view>
</block>
<!-- 否則顯示沒有更多內(nèi)容了 -->
<block wx:else>
<view class="load-content">
<text>沒有更多內(nèi)容了</text>
</view>
</block>
</view>
index.js
Page({
data: {
listdata:[], //數(shù)據(jù)
moredata: '',
p:0, //當前分頁;默認第一頁
hasMore:true //提示
},
//加載初始數(shù)據(jù)
onLoad: function (options) {
var that = this;
//初始頁面
var p = that.data.p;
this.loadmore();
},
//觸底事件
onReachBottom:function(){
var that = this;
//檢查是否還有數(shù)據(jù)可以加載
var moredata = that.data.moredata;
//如果還有,則繼續(xù)加載
if (moredata.more != 0) {
this.loadmore();
//如果沒有了,則停止加載,顯示沒有更多內(nèi)容了
}else{
that.setData({
"hasMore": false
})
}
},
//發(fā)起請求
loadmore:function(){
//加載提示
wx.showLoading({
title: '加載中',
})
var that = this;
//頁面累加
var p = ++that.data.p;
//請求服務(wù)器
wx.request({
url: '你的服務(wù)器/server.php?page=' + p,
data: {
"json": JSON.stringify({
"p": p
})
},
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
//請求成功,回調(diào)函數(shù)
success:function(res){
//隱藏加載提示
wx.hideLoading();
//判斷市局是否為空
if (res.data != 0) {
that.setData({
//把新加載的數(shù)據(jù)追加到原有的數(shù)組
"listdata": that.data.listdata.concat(res.data), //加載數(shù)據(jù)
"moredata": res.data,
"p":p
})
} else {
that.setData({
"hasMore":false
})
}
}
})
}
})
說明
1、url修改為你的服務(wù)端鏈接,格式是
http:域名/目錄/?page=頁碼
例如:
http://www.baidu.com/api/data.php?page=1
頁碼是可變的,所以聲明一個變量p,所以就是
`url: 'http://www.baidu.com/api/data.php?page' + p,`
index.wxss
.listitem{
width: 90%;
height: 155px;
background: rgba(0, 0, 0, 0.2);
margin:10px auto;
text-align: center;
position: relative;
color:#fff;
}
.listitem .cover{
width:100%;
height:155px;
position: absolute;
top: 0;
left: 0;
z-index: -100;
}
.load-more-wrap .load-content{
text-align: center;
margin:30px auto 30px;
color:#ccc;
font-size: 15px;
}
服務(wù)端返回的數(shù)據(jù)格式
返回json數(shù)組的形式,例如
[
{"id":"1","title":"標題1","coverimg":"url1"},
{"id":"2","title":"標題2","coverimg":"url2"},
{"id":"3","title":"標題3","coverimg":"url3"},
{"id":"4","title":"標題4","coverimg":"url4"},
{"id":"5","title":"標題5","coverimg":"url5"}
]
希望本文所述對大家微信小程序設(shè)計有所幫助。
相關(guān)文章
微信小程序?qū)崿F(xiàn)打開內(nèi)置地圖功能【附源碼下載】
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)打開內(nèi)置地圖功能,涉及微信小程序使用wx.openLocation函數(shù)獲取經(jīng)緯度信息的相關(guān)使用技巧,并附帶源碼供讀者下載參考,需要的朋友可以參考下2017-12-12
JS如何將數(shù)字金額轉(zhuǎn)換成中文金額格式
這篇文章主要介紹了JS如何將數(shù)字金額轉(zhuǎn)換成中文金額格式,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07

