Blogger Information
Blog 19
fans 0
comment 1
visits 19481
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
微信小程序上拉加载 加载更多数据 触底加载 点击加载更多数据
TANKING的代码日志
Original
1664 people have browsed it

开发需求

进入页面,加载初始数据,当向上拖动页面至底部,自动加载新的数据,即上拉加载更多数据。

演示

index.wxml

  1. <!-- 数据列表 -->
  2. <view wx:for="{{listdata}}" wx:key="listdata" class='listitem'>
  3. <view class='title'>{{item.title}}</view>
  4. <view class='title'>资源ID:{{item.id}}</view>
  5. <image src="{{item.coverimg}}" class='cover'></image>
  6. </view>
  7. <!-- 如果还有更多数据可以加载,则显示玩命加载中 -->
  8. <view class="load-more-wrap">
  9. <block wx:if="{{hasMore}}">
  10. <view class="load-content">
  11. <text class="weui-loading"/><text class="loading-text">玩命加载中</text>
  12. </view>
  13. </block>
  14. <!-- 否则显示没有更多内容了 -->
  15. <block wx:else>
  16. <view class="load-content">
  17. <text>没有更多内容了</text>
  18. </view>
  19. </block>
  20. </view>

index.js

  1. Page({
  2. data: {
  3. listdata:[], //数据
  4. moredata: '',
  5. p:0, //当前分页;默认第一页
  6. hasMore:true //提示
  7. },
  8. //加载初始数据
  9. onLoad: function (options) {
  10. var that = this;
  11. //初始页面
  12. var p = that.data.p;
  13. this.loadmore();
  14. },
  15. //触底事件
  16. onReachBottom:function(){
  17. var that = this;
  18. //检查是否还有数据可以加载
  19. var moredata = that.data.moredata;
  20. //如果还有,则继续加载
  21. if (moredata.more != 0) {
  22. this.loadmore();
  23. //如果没有了,则停止加载,显示没有更多内容了
  24. }else{
  25. that.setData({
  26. "hasMore": false
  27. })
  28. }
  29. },
  30. //发起请求
  31. loadmore:function(){
  32. //加载提示
  33. wx.showLoading({
  34. title: '加载中',
  35. })
  36. var that = this;
  37. //页面累加
  38. var p = ++that.data.p;
  39. //请求服务器
  40. wx.request({
  41. url: '你的服务器/server.php?page=' + p,
  42. data: {
  43. "json": JSON.stringify({
  44. "p": p
  45. })
  46. },
  47. method: 'POST',
  48. header: {
  49. 'content-type': 'application/x-www-form-urlencoded'
  50. },
  51. //请求成功,回调函数
  52. success:function(res){
  53. //隐藏加载提示
  54. wx.hideLoading();
  55. //判断市局是否为空
  56. if (res.data != 0) {
  57. that.setData({
  58. //把新加载的数据追加到原有的数组
  59. "listdata": that.data.listdata.concat(res.data), //加载数据
  60. "moredata": res.data,
  61. "p":p
  62. })
  63. } else {
  64. that.setData({
  65. "hasMore":false
  66. })
  67. }
  68. }
  69. })
  70. }
  71. })

index.wxss

  1. .listitem{
  2. width: 90%;
  3. height: 155px;
  4. background: rgba(0, 0, 0, 0.2);
  5. margin:10px auto;
  6. text-align: center;
  7. position: relative;
  8. color:#fff;
  9. }
  10. .listitem .cover{
  11. width:100%;
  12. height:155px;
  13. position: absolute;
  14. top: 0;
  15. left: 0;
  16. z-index: -100;
  17. }
  18. .load-more-wrap .load-content{
  19. text-align: center;
  20. margin:30px auto 30px;
  21. color:#ccc;
  22. font-size: 15px;
  23. }

服务端返回的数据格式

  1. [
  2. {"id":"1","title":"标题1","coverimg":"url1"},
  3. {"id":"2","title":"标题2","coverimg":"url2"},
  4. {"id":"3","title":"标题3","coverimg":"url3"},
  5. {"id":"4","title":"标题4","coverimg":"url4"},
  6. {"id":"5","title":"标题5","coverimg":"url5"}
  7. ]

作者

Author:TANKING
Date:2020-04-20
Web:www.likeyunba.com
WeChat:face6009

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
1 comments
一生的承诺 2022-10-05 12:55:41
用了这个代码循环出来了,但是让4条数据读取,但是不停地还是这4条数据循环读出来呢
1 floor
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!