【小程序】分页获取数据

const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    cusor: 0, //当前栏目
    status: [],
    type: ['未使用', '已使用', '已過期'],
    list: [], //全部列表数据
    showList: [], //当前展示数据
    currentTime: Date.parse(new Date()) / 1000, //当前时间戳

    currentPage: 1,
    noMore: false,
    pageBackgroundColor: "#fff", //页面背景颜色
  },
  // 切换栏目
  handtab(e) {
    this.setData({
      cusor: e.currentTarget.dataset.index,
    })
    // 获取当前栏目数据
    this.getList(false, this.data.cusor)
  },
  // 获取数据
  getList(isLoadMore = false, index) {
    // 初始化展示列表
    this.data.showList = []
    wx.showLoading({
      title: '加載中...',
    })

    const currentPage = isLoadMore ? this.data.currentPage + 1 : 1;
    api.baseRequest('/', 'POST', {
      page: currentPage
    }, true).then(res => {
      wx.stopPullDownRefresh();
      wx.hideLoading()
      if (res.code == 200) {
        var results = res.data
        if (currentPage > 1) {
          results = this.data.list.concat(results);
        }
        this.setData({
          list: results,
          currentPage: currentPage,
          noMore: res.data.length < 10,
          pageBackgroundColor: "#f4f6f7"
        })
      } else {
        this.setData({
          pageBackgroundColor: "#fff"
        })
      }

      var _arr = this.data.showList
      // 遍历寻找符合条件数据
      this.data.list.forEach((item) => {
        // 未使用
        if (index == 0) {
          if (item.w_status == 0) {
            _arr.push(item)
          }
          // 已使用
        } else if (index == 1) {
          if (item.w_status == 1) {
            _arr.push(item)
          }
          // 已过期
        } else if (index == 2) {
          if (item.w_status == 2) {
            _arr.push(item)
          }
        }
      })
      this.setData({
        showList: _arr
      })
      console.log(this.data.showList);
    })
  },
  // 获取时间戳差
  getDiffTime(endTime) {
    //获取当前时间戳
    var newDateTime = Date.parse(new Date())
    console.log(newDateTime)
    //指定的时间戳
    var datetime = endTime;
    console.log(formatTime(newDateTime))
    console.log(formatTime(datetime))

    //时间戳相差多少秒
    console.log((newDateTime - datetime) / 1000)

    //时间戳转换为时间
    function formatTime(newDateTime) {
      var date = new Date(newDateTime)
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      var day = date.getDate();
      var hour = date.getHours();
      var minute = date.getMinutes();
      var second = date.getSeconds();
      return year + "年" + month + "月" + day + "日" + hour + "时" + minute + "分" + second + "秒"
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getList(false,this.data.cusor); //获取数据
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getList(false, this.data.cusor);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.noMore == false) {
      this.getList(true, this.data.cusor);
    }
  },
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值