DIV CSS 佈局教程網

微信小程序實作加載更多、點擊查看更多
編輯:關於JavaScript     

本文為大家分享微信小程序「加載更多」功能的實作程式碼,供參考,具體內容如下。

微信小程序的加載更多,核心在於把先前的資料與點擊加載後請求到的資料用 concat 拼接在一起,再執行 setData。以下是一個簡單的例子:

index.wxml 程式碼如下:

 <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 檔案程式碼如下:

 Page({ data: { loadText:'加載更多', duanziInfo:[] }, //初始化請求 onLoad: function (res) { var that = this //內容 wx.request({ url: 'https://xxxxx.com/index.php?m=Industry&a=getDuanziInfo', data: {token:token}, method: 'GET', 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: '加載中', icon: 'loading', duration: 200 }) wx.request({ url: 'https://xxxxx.com/index.php?m=Industry&a=getDuanziInfo', data: {token:token}, method: 'GET', success: function(res){ console.log(duanziInfoBefore.concat(res.data.result)) //打印拼接之後數據 that.setData({ loadText:"數據請求中", loading:true, duanziInfo:duanziInfoBefore.concat(res.data.result), loadText:"加載更多", loading:false, }) } }) } }) 

初始化與加載更多的列印資料如下:

(以上是點擊查看更多;此外也可以依據與視圖區域的距離來觸發加載更多,具體做法是將視圖用 <scroll-view> 標籤包裝並給定固定高度,再於參數中設定觸發事件的距離像素。相關文件:https://mp.weixin.qq.com/debug/wxadoc/dev/component/scroll-view.html?t=20161122)

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved