微信小程序中加载更多与点击查看更多的代码剖析

why 132 2024-08-21

这篇文章主要为大家详细介绍了微信小程序加载更多,点击查看更多功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了微信小程序加载更多功能实现的具体代码,供大家参考,具体内容如下

微信小程序加载更多,是将之前的数据和点击加载后请求的数据用concat拼接在一起并执行setData,下面是一个简单的栗子:

index.wxml代码如下


1

2

3

4

5

6

7

8

9

10

11

12

<view wx:for="{{duanziInfo}}" wx:for-item="name" wx:for-index="id">

 <view class="duanzi-view">

 <view class="duanzi-content">

 <text class="dz-content">{{name.content}}</text>

 </view>

 </view>

</view>

<view class="button-wrapper">

 <button type="default" size="default" loading="{{loading}}" disabled="{{disabled}}" bindtap="setLoading">

 {{loadText}}

 </button>

</view>


加载更多按钮绑定setLoading

index.js文件代码如下


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

Page({

 data: {

  loadText:&#39;加载更多&#39;,

  duanziInfo:[]

 },

 //初始化请求

 onLoad: function (res) {

 var that = this

 //内容

 wx.request({

  url: &#39;http://xxxxx.com/index.php?m=Industry&a=getDuanziInfo&#39;,

  data: {token:token},

  method: &#39;GET&#39;,

  success: function(res){

  console.log(res.data.result) //打印初始化数据

  that.setData({

  duanziInfo:res.data.result

  })

  }

 })

 },

 //加载更多

 setLoading: function(e) {

 var duanziInfoBefore = this.data.duanziInfo

 var that = this

 wx.showToast({ //期间为了显示效果可以添加一个过度的弹出框提示“加载中”

  title: &#39;加载中&#39;,

  icon: &#39;loading&#39;,

  duration: 200

  })

 wx.request({

  url: &#39;http://xxxxx.com/index.php?m=Industry&a=getDuanziInfo&#39;,

  data: {token:token},

  method: &#39;GET&#39;,

  success: function(res){

  console.log(duanziInfoBefore.concat(res.data.result)) //打印拼接之后数据

  that.setData({

  loadText:"数据请求中",

  loading:true,

  duanziInfo:duanziInfoBefore.concat(res.data.result),

  loadText:"加载更多",

  loading:false,

  })

  }

 })

 }

})


初始化和加载更多中的打印数据如下

image.png

(以上是点击查看更多,还可以根据距离视图区域的距离来加载更多,具体实现是将视图用标签,并给其一个固定高度,在给定参数中设置距离像素触发事件。具体文档:https://mp.weixin.qq.com/debug/wxadoc/dev/component/scroll-view.html?t=20161122)

以上就是本文的全部内容,希望对大家的学习有所帮助。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:概述微服务中间件
下一篇:微信小程序内顶部导航栏的有效实现方式解析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~