微信小程序 刷新加载更多

微信小程序 刷新加载更多

wxml:
<view class="search">

  <view class="search-result">
    <scroll-view scroll-y="true" bindscrolltolower="searchScrollLower">
      <view class="result-item" wx:for="{{searchResultList}}" wx:key="unique" data-data="{{item}}">
      
        <image class="image_frame" mode="scaleToFill" src="{{item.img}}" />
        <view class='view_content'>
          <text>{{item.title}}</text>
          <text>{{item.profile}}</text>
        </view>


      </view>
      <view class="loading" hidden="{{!searchLoading}}">正在载入更多...</view>
      <view class="loading complete" hidden="{{!searchLoadingComplete}}">已加载全部</view>
    </scroll-view>
  </view>
</view>
js:
var util = require('../../utils/pulldown.js')

Page({

  data: {
    searchKeyword: '', //需要搜索的字符
    searchResultList: [], //放置返回数据的数组
    isFromSearch: true, // 用于判断searchSongList数组是不是空数组,默认true,空的数组
    searchPageNum: 1, // 设置加载的第几次,默认是第一次
    callbackcount: 10, //返回数据的个数
    searchLoading: false, //"上拉加载"的变量,默认false,隐藏
    searchLoadingComplete: false //“没有数据”的变量,默认false,隐藏
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      searchPageNum: 1, //第一次加载,设置1
      searchSongList: [], //放置返回数据的数组,设为空
      isFromSearch: true, //第一次加载,设置true
      searchLoading: true, //把"上拉加载"的变量设为true,显示
      searchLoadingComplete: false //把“没有数据”设为false,隐藏
    })
    // 显示导航栏loading
    wx.showNavigationBarLoading();
    // 调用接口加载数据
    this.fetchSearchList();
    // 隐藏导航栏loading
    wx.hideNavigationBarLoading();
  },

  // 下拉刷新
  onPullDownRefresh: function() {
    this.setData({
      searchPageNum: 1, //第一次加载,设置1
      searchSongList: [], //放置返回数据的数组,设为空
      isFromSearch: true, //第一次加载,设置true
      searchLoading: true, //把"上拉加载"的变量设为true,显示
      searchLoadingComplete: false //把“没有数据”设为false,隐藏
    })
    // 显示导航栏loading
    wx.showNavigationBarLoading();
    // 调用接口加载数据
    this.fetchSearchList();
    // 隐藏导航栏loading
    wx.hideNavigationBarLoading();
    // 当处理完数据刷新后,wx.stopPullDownRefresh可以停止当前页面的下拉刷新
    wx.stopPullDownRefresh();
  },
  // 上拉加载
  onReachBottom(e) {
    let that = this;
    if (that.data.hasMore) {
      that.setData({
        searchPageNum: that.data.searchPageNum + 1, // 每次触发上拉事件,把pageNum+1
        isFromSearch: false // 触发到上拉事件,把isFirstLoad设为为false
      });

      that.fetchSearchList();
    }
  },

  //搜索,访问网络
  fetchSearchList: function() {
    let that = this;
    let searchKeyword = that.data.searchKeyword, //输入框字符串作为参数
      searchPageNum = that.data.searchPageNum, //把第几次加载次数作为参数
      callbackcount = that.data.callbackcount; //返回数据的个数
    //访问网络
    util.getSearch(searchKeyword, searchPageNum, callbackcount, function(data) {
      console.log(data)
      //判断是否有数据,有则取数据
      if (data.d.length != 0) {
        let searchList = [];
        //如果isFromSearch是true从data中取出数据,否则先从原来的数据继续添加
        that.data.isFromSearch ? searchList = data.d : searchList = that.data.searchResultList.concat(data.d)

        that.setData({
          searchResultList: searchList, //获取数据数组
          searchLoading: true //把"上拉加载"的变量设为false,显示
        });

        console.log("searchResultList = " + that.data.searchResultList);
        //没有数据了,把“没有数据”显示,把“上拉加载”隐藏
      } else {
        that.setData({
          searchLoadingComplete: true, //把“没有数据”设为true,显示
          searchLoading: false //把"上拉加载"的变量设为false,隐藏
        });
      }
    })

  },
 
  //滚动到底部触发事件
  searchScrollLower: function() {
    let that = this;
    if (that.data.searchLoading && !that.data.searchLoadingComplete) {
      that.setData({
        searchPageNum: that.data.searchPageNum + 1, //每次触发上拉事件,把searchPageNum+1
        isFromSearch: false //触发到上拉事件,把isFromSearch设为为false
      });
      that.fetchSearchList();
    }
  }
})
pulldown.js
function getSearch(keyword, pageindex, callbackcount, callback) {
  wx.request({
    url: 'https://xiaoce-timeline-api-ms.juejin.im/v1/getListByLastTime?pageNum=' + pageindex,
    data: [],
    method: 'GET',
    header: {
      'content-Type': 'application/json'
    },
    success: function(res) {
      if (res.statusCode == 200) {
        callback(res.data);
      }
    }
  })
}

module.exports = {
  getSearch: getSearch
}
注意

要先配置request,要不然报错

22.png
11.png
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 转载链接 注:本文转载知乎上的回答 作者:初雪 链接:https://www.zhihu.com/question...
    pengshuangta阅读 29,623评论 9赞 295
  • 1. 收到颜冬要结婚的信息时,陈辰一口把嘴里的香菜吐了出来。静默了一会儿,又擦擦手机屏幕,划开锁屏再次确认。 “各...
    earth辰夕阅读 904评论 11赞 8
  • 杜曼媛阅读 201评论 0赞 0
  • 每次我来“大姨妈”的时候,都感到生无可恋。能够获得理解是一件多么艰难的事情,比活着更加艰难。我尝试过改变,甚至...
    雾隐青山阅读 481评论 1赞 0
  • 20180725周三自由书写作业 害怕工作 一提起工作 就想逃避 不愿意想 不愿意碰 当我把注意力放在工作这件事上...
    write1st阅读 197评论 0赞 0

友情链接更多精彩内容