轻源码

  • QingYuanMa.com
  • 全球最大的互联网技术和资源下载平台
搜索
一起源码网 门户 微信小程序 查看主题

阿东入门系列《五》仿找事吧APP附近三公里Demo

发布者: mzlihui | 发布时间: 2017-11-14 03:40| 查看数: 2166| 评论数: 1|帖子模式

源码下载地址 :Github

效果图如下:

分析一下页面,主要内容分为顶部轮播,中间10个分类图标的排版和单击事件,下部列表下拉刷新上拉加载更多。大部分知识点前面都讲过。这里主要说一下微信小程序中的数据绑定,前后台传值以及加载更多时的数据合并。

1. 数据绑定和前后台传值

中间分类图标的布局文件:

  1. <view class="items" wx:for="{{array}}" wx:for-item="item" bindtap="typeclick" data-code="{{item.code}}" data-text="{{item.text}}" >
  2. <image class="item-img" mode="aspectFit" src="{{item.src}}"></image>
  3. <view class="item-text">{{item.text}}</view>
  4. </view>

可以看出是以 控制属性 wx:for 绑定数据 array 来循环渲染布局,并对view绑定了单击事件bindtap="typeclick"。因为每一个分类点击都会刷新下部列表,所以需要在事件中获得当前分类数据的code。小程序中提供自定义标签 data-XXX,供开发者使用来绑定数据,XXX 可以随意取名,这里我们用 data-code="{{item.code}}" data-text="{{item.text}}"把每条数据的code和text传给function typeclick

然后在js中的 typeclick 函数中,我们可以通过event拿到绑定的数据。

  1. // 分类item单击事件
  2. typeclick: function (e) {
  3. total = 0;
  4. code = e.currentTarget.dataset.code + "";
  5. var name = e.currentTarget.dataset.text + "";
  6. this.data.dataArray = [];
  7. this.setData({
  8. title: "附近三公里: " + name
  9. })
  10. this.periphery();
  11. },

e.currentTarget.dataset.code 后边的code就是我们在布局文件中定义的 data-XXX 中的XXX,这里需要注意一下,因为js的机制,有时候我们拿到的数据类型可能不对,需要自己处理一下。

2. 加载更多时的数据合并

  1. // 网络请求
  2. periphery: function () {
  3. var that = this
  4. //sliderList
  5. wx.request({
  6. url: 'http://xxx',
  7. method: 'POST',
  8. data: {
  9. city: "深圳",
  10. code: code,
  11. count: count + "",
  12. total: total + "",
  13. lat: app.globalData.latitude + "",
  14. lng: app.globalData.longitude + ""
  15. },
  16. header: {
  17. 'Accept': 'application/json'
  18. },
  19. success: function (res) {
  20. that.data.dataArray = that.data.dataArray.concat(res.data.data.list)
  21. that.setData({
  22. dataArray: that.data.dataArray
  23. })
  24. setTimeout(function () {
  25. that.setData({
  26. loadingHidden: true
  27. })
  28. }, 1000)
  29. }
  30. })
  31. },

因为列表有上拉刷新和下拉加载更多的功能。所以每次的网络请求通过 total和count控制每次请求的数据的页码,然后在 success 回调中把数据拼接到原来的数据集合上。

首先注意一点。在wx.request的回调中,我们不能直接用this.data.dataArray 来取data标签下的dataArray,因为这里的this代表的并不是js的全局上下文对象,他对应的是这个function的上下文。所以我们需要在 wx.request 的外部,通过一个变量来保存js的全局上下文对象,var that = this ,然后在回调中用 that.data.dataArray

然后说数据拼接,需要用到concat 关键字,他可以把其参数拼接到调用者身上。that.data.dataArray.concat(res.data.data.list) 这里需要注意请求返回的数据格式,res.data代表的是返回的json,然后自己根据数据格式拼接,直到取到数据集合。

其次因为上拉和下拉的性质不同,其处理方式也不同,下拉需要把数据集合置为空并从头开始去数据。上拉需要处理total,来取下一个count条数的数据。代码如下:

  1. // 下拉刷新回调接口
  2. onPullDownRefresh: function () {
  3. total = 0;
  4. this.data.dataArray = [];
  5. this.periphery();
  6. wx.stopPullDownRefresh;
  7. },
  8. // 上拉加载回调接口
  9. onReachBottom: function () {
  10. total += count;
  11. this.periphery();
  12. },

下面附上完整的代码:

  1. <!--main.wxml-->
  2. <view>
  3. <swiper class="swiper_box" indicator-dots="{{indicatorDots}}" vertical="{{vertical}}"
  4. autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}" bindchange="swiperchange">
  5. <block wx:for="{{images}}">
  6. <swiper-item bindtap="itemclick" data-id="{{item.img}}" data-name="{{item.name}}">
  7. <image src="{{item.img}}" class="slide-image"/>
  8. </swiper-item>
  9. </block>
  10. </swiper>
  11. </view>
  12. <!--nearby.wxml-->
  13. <scroll-view class="sv" scroll-y="true">
  14. <view style="overflow:hidden;">
  15. <view class="items" wx:for="{{array}}" wx:for-item="item" bindtap="typeclick" data-code="{{item.code}}" data-text="{{item.text}}" >
  16. <image class="item-img" mode="aspectFit" src="{{item.src}}"></image>
  17. <view class="item-text">{{item.text}}</view>
  18. </view>
  19. </view>
  20. <view class="data">
  21. <text class="data-title">{{title}}</text>
  22. <view style="overflow:hidden;">
  23. <view class="data-items" wx:for="{{dataArray}}" wx:for-item="item" wx:key="id" bindtap="openmap"
  24. data-lat="{{item.lat}}" data-lng="{{item.lng}}" data-name="{{item.name}}" data-address="{{item.address}}">
  25. <image class="data-item-img" mode="aspectFit" src="{{item.img}}"></image>
  26. <view class="data-item-text">
  27. <view style="width:100%; font-size: 30rpx; padding:2rpx;">{{item.name}}</view>
  28. <view style="width:100%; font-size: 25rpx; padding:2rpx;">{{item.address}}</view>
  29. <view style="width:100%; font-size: 25rpx; padding:2rpx;">{{item.phone}}</view>
  30. </view>
  31. </view>
  32. </view>
  33. </view>
  34. </scroll-view>
  35. <loading hidden="{{loadingHidden}}">
  36. 加载中...
  37. </loading>
  1. /**main.wxss**/
  2. .swiper_box {
  3. width: 100%;
  4. }
  5. swiper-item image {
  6. width: 100%;
  7. display: inline-block;
  8. overflow: hidden;
  9. }
  10. .sv{
  11. background-color:#efeff4;
  12. margin-top: 10rpx
  13. }
  14. .items{
  15. float:left;
  16. width: 20%;
  17. background-color:#fff;
  18. }
  19. .item-img{
  20. width: 100%;
  21. height: 60rpx;
  22. }
  23. .item-text{
  24. width: 100%;
  25. height: 60rpx;
  26. font-size: 25rpx;
  27. text-align:center;
  28. }
  29. .data{
  30. margin-top: 10rpx;
  31. background-color:#fff;
  32. padding: 10rpx;
  33. }
  34. .data-title{
  35. padding-left: 10rpx;
  36. padding-top: 15rpx;
  37. }
  38. .data-items{
  39. width: 100%;
  40. margin-top: 10rpx;
  41. margin-bottom: 10rpx;
  42. overflow: hidden;
  43. }
  44. .data-item-img{
  45. width: 20%;
  46. height:120rpx;
  47. float:left;
  48. }
  49. .data-item-text{
  50. width: 75%;
  51. padding: 5rpx;
  52. height:120rpx;
  53. float:left;
  54. }
  1. //main.js
  2. //获取应用实例
  3. var app = getApp()
  4. var count = 10;
  5. var total = 0;
  6. var code = "2";
  7. Page({
  8. data: {
  9. title: "附近三公里",
  10. indicatorDots: true,
  11. vertical: false,
  12. autoplay: true,
  13. interval: 3000,
  14. duration: 1000,
  15. loadingHidden: false, // loading
  16. array: [{
  17. code: '1',
  18. id: 'icon_1',
  19. src: 'http://xxx',
  20. text: '家政'
  21. }, {
  22. code: '2',
  23. id: 'icon_2',
  24. src: 'http://xxx',
  25. text: '药店'
  26. }, {
  27. code: '3',
  28. id: 'icon_3',
  29. src: 'http://xxx',
  30. text: '银行'
  31. }, {
  32. code: '4',
  33. id: 'icon_4',
  34. src: 'http://xxx',
  35. text: '维修'
  36. }, {
  37. code: '5',
  38. id: 'icon_5',
  39. src: 'http://xxx',
  40. text: '公厕'
  41. }, {
  42. code: '6',
  43. id: 'icon_6',
  44. src: 'http://xxx',
  45. text: '医院'
  46. }, {
  47. code: '7',
  48. id: 'icon_7',
  49. src: 'http://xxx',
  50. text: '加油站'
  51. }, {
  52. code: '8',
  53. id: 'icon_8',
  54. src: 'http://xxx',
  55. text: '汽车洗护'
  56. }, {
  57. code: '9',
  58. id: 'icon_9',
  59. src: 'http://xxx',
  60. text: '营业厅'
  61. }, {
  62. code: '10',
  63. id: 'icon_10',
  64. src: 'http://xxx',
  65. text: '停车场'
  66. }],
  67. dataArray: []
  68. },
  69. //事件处理函数
  70. swiperchange: function (e) {
  71. // 此处写 轮播 改变时会触发的 change 事件
  72. },
  73. // 轮播item点击事件
  74. itemclick: function (e) {
  75. wx.showToast({
  76. title: e.currentTarget.dataset.id + "",
  77. icon: 'success',
  78. duration: 2000
  79. })
  80. },
  81. // 分类item单击事件
  82. typeclick: function (e) {
  83. total = 0;
  84. code = e.currentTarget.dataset.code + "";
  85. var name = e.currentTarget.dataset.text + "";
  86. this.data.dataArray = [];
  87. this.setData({
  88. title: "附近三公里: " + name
  89. })
  90. this.periphery();
  91. },
  92. onLoad: function () {
  93. console.log('onLoad')
  94. var that = this
  95. count = 10;
  96. total = 0;
  97. //sliderList
  98. wx.request({
  99. url: 'http://xxx',
  100. method: 'POST',
  101. data: {
  102. type: "1"
  103. },
  104. header: {
  105. 'Accept': 'application/json'
  106. },
  107. success: function (res) {
  108. that.setData({
  109. images: res.data.data.guanggao
  110. })
  111. }
  112. })
  113. this.periphery();
  114. },
  115. // 网络请求
  116. periphery: function () {
  117. var that = this
  118. //sliderList
  119. wx.request({
  120. url: 'http://xxx',
  121. method: 'POST',
  122. data: {
  123. city: "深圳",
  124. code: code,
  125. count: count + "",
  126. total: total + "",
  127. lat: app.globalData.latitude + "",
  128. lng: app.globalData.longitude + ""
  129. },
  130. header: {
  131. 'Accept': 'application/json'
  132. },
  133. success: function (res) {
  134. that.data.dataArray = that.data.dataArray.concat(res.data.data.list)
  135. that.setData({
  136. dataArray: that.data.dataArray
  137. })
  138. setTimeout(function () {
  139. that.setData({
  140. loadingHidden: true
  141. })
  142. }, 1000)
  143. }
  144. })
  145. },
  146. // 下拉刷新回调接口
  147. onPullDownRefresh: function () {
  148. total = 0;
  149. this.data.dataArray = [];
  150. this.periphery();
  151. wx.stopPullDownRefresh;
  152. },
  153. // 上拉加载回调接口
  154. onReachBottom: function () {
  155. total += count;
  156. this.periphery();
  157. },
  158. openmap: function (e) {
  159. wx.openLocation({
  160. latitude: e.currentTarget.dataset.lat , // 纬度,范围为-90~90,负数表示南纬
  161. longitude: e.currentTarget.dataset.lng, // 经度,范围为-180~180,负数表示西经
  162. scale: 28, // 缩放比例
  163. name: e.currentTarget.dataset.name, // 位置名
  164. address: e.currentTarget.dataset.address, // 地址的详细说明
  165. success: function(res){
  166. // success
  167. },
  168. fail: function() {
  169. // fail
  170. },
  171. complete: function() {
  172. // complete
  173. }
  174. })
  175. },
  176. })

main.json

  1. {
  2. "enablePullDownRefresh": true
  3. }

最新评论

海天黄豆酱 发表于 2022-5-3 09:43
html css网页源码

轻源码让程序更轻更快

QingYuanMa.com

工作时间 周一至周六 8:00-17:30

侵权处理

客服QQ点击咨询

关注抖音号

定期抽VIP

Copyright © 2016-2021 https://www.171739.xyz/ 滇ICP备13200218号

快速回复 返回顶部 返回列表