1. 程式人生 > >微信小程序 - (下拉)加載更多數據

微信小程序 - (下拉)加載更多數據

class success request etl isa icon ica 16px gets

技術分享圖片

註意和後端配合就行了,前端也只能把數據拼接起來!

無論是下拉加載還是加載更多,一樣的道理!

註意首次加載傳遞參數

註意每次加載數據數

wxml

    <view class=‘table-rank‘>
      <view class=‘tables center‘ wx:for="{{ranklist}}" wx:for-index="idx" wx:key="prototype">
        <view class=‘stage-rank‘>
          {{idx+1}}
        </view>
        <view class=‘name‘>{{item.name}}</view>
        <view class=‘price‘>{{item.count}}</view>
      </view>
    </view>

    <view class=‘more‘>
      <button loading="{{loading}}" disabled="{{disabled}}" catchtap="setLoading">{{loadText}}</button>
    </view>

js

var app = getApp().globalData;

Page({
  data: {
    loadText: ‘加載更多‘,
    ranklist: [],
    count: 1
  },


  onLoad: function(res) {
    // 首次加載:傳參數num:0
    let num = 0;
    let _page = this;
    let uid = wx.getStorageSync(‘uinfo‘).uid;


    wx.request({
      url: ‘url‘,
      data: {
        uid,
        num
      },
      header: {
        ‘content-type‘: ‘application/json‘
      },
      method: ‘GET‘,
      dataType: ‘json‘,
      responseType: ‘text‘,
      success(res) {
        _page.setData({
          ranklist: res.data.all_list,
me: res.data.me }); } }) }, //點擊 加載更多 按鈕 setLoading(e) { let _this = this; let _page = this; // 暫存數據 let ranklistBefore = _this.data.ranklist; let uid = wx.getStorageSync(‘uinfo‘).uid; // 每次加載數據條數(10) let num = _this.data.count++ * 10; wx.request({ url: app.api.simulation_ranking, data: { uid, num }, header: { ‘content-type‘: ‘application/json‘ }, method: ‘GET‘, dataType: ‘json‘, responseType: ‘text‘, success(res) { // 每次加載數據,請求一次就發送10條數據過來 let eachData = res.data.all_list; if (eachData.length == 0) { wx.showToast({ title: ‘沒有更多數據了!~‘, icon: ‘none‘ }) } else { wx.showToast({ title: ‘數據加載中...‘, icon: ‘none‘ }) } _page.setData({ loadText: "數據請求中", loading: true, ranklist: ranklistBefore.concat(eachData),
loadText: "加載更多", loading: false, }); } }) } });

  

微信小程序 - (下拉)加載更多數據