微信小程序下拉加载更多 微信小程序实现下拉加载更多商品
保护我方豆豆 人气:01. source code
<view class='goods'> <view class='good' wx:for="{{ goodslist }}" wx:key='index'> <view class='pic'> <image src='{{ item.imgUrl }}'></image> </view> <view class='title'> {{ item.des }} </view> <view class='desc'> <text class='price'>¥{{ item.price }}</text> <text class='paynum'> {{ item.alreadyPaid }} </text> </view> </view> </view> <button loading wx:if="{{loadmore}}"> loading... </button> <button wx:else> 我是有底线的 </button>
wxss:
/* pages/loadmore/loadmore.wxss */ .goods{ display: flex; justify-content: space-between; flex-wrap: wrap; margin-top: 20rpx; background-color: #ddd; } .good{ width: 370rpx; height: 528rpx; /* background-color: red; */ margin-bottom: 20rpx; } .pic{ width: 370rpx; height: 370rpx; } .pic image{ width: 100%; height: 100%; } .title{ font-size: 26rpx; padding: 20rpx; height: 52rpx; overflow: hidden; } .desc{ font-size: 23rpx; padding: 20rpx; } .paynum{ margin-left: 165rpx; }
js:
1
// pages/loadmore/loadmore.js Page({ /** * 页面的初始数据 */ data: { data: [], // 所有数据 goodslist:[], // 展示数据 loadmore: true }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { const that = this; wx.request({ url: 'http://www.tanzhouweb.com/vueProject/vue.php?title=likeYou', success(res){ const data = res.data; const goodslist = []; // 初始显示6条数据 data.forEach((item, index) => { if(index<6){ goodslist.push(item) } }); // console.log(data) that.setData({ goodslist, data }) } }) }, // 下拉触底执行(下拉触底添加后6条数据,直到所有数据加载完成) onReachBottom(e){ const {data, goodslist} = this.data; const start = goodslist.length; const end = Math.min( start + 6, data.length - 1); if(goodslist.length == data.length){ this.setData({ loadmore:false }) return ; } for(let i = start; i<=end; i++){ goodslist.push(data[i]) } this.setData({ goodslist }) } })
{ "usingComponents": {}, "navigationBarBackgroundColor": "#3366CC", "navigationBarTitleText": "商品加载", "navigationBarTextStyle": "white" }
2. 效果
初始显示6条数据,下拉触底加载后6条数据
生命周期函数: onLoad onReachBottom
加载全部内容