search
HomeWeChat AppletMini Program DevelopmentA brief discussion on how to implement the pull-down refresh and pull-up loading functions in mini programs? (with code)

This article will introduce to you how to implement the pull-down refresh and pull-up loading functions in the mini program. I hope it will be helpful to you!

A brief discussion on how to implement the pull-down refresh and pull-up loading functions in mini programs? (with code)

When displaying list data, if there is a lot of data or it updates quickly, you need to provide pull-up refresh and pull-down loading functions to improve the user experience. [Related learning recommendations: 小program development tutorial]

Pull down to refresh and pull up to load wxml file writing

When we use scroll-view to slide When the component displays the list, it itself has trigger functions for pull-down refresh and pull-up loading

<scroll-view class="scroll" scroll-y="{{true}}" upper-threshold="50"   
bindscrolltoupper="refresh"  style="height:700px">
  <l-loadmore show="{{upfresh}}" bindscrolltolower="getMore"  type="loading" loading-text="拼命刷新中">
</l-loadmore>

<l-loadmore show="{{downfresh}}" type="loading" loading-text="拼命加载中">
</l-loadmore>
  • scroll-y: Whether to allow vertical scrolling, the default is false, here we set it to true
  • upper-threshold: How far away from the top/left is, the scrolltoupper event is triggered (pull down to refresh)
  • bindscrolltoupper: Triggered when scrolling to the top/left, here set the function that needs to be triggered when scrolling to the top
  • bindscrolltolower: Triggered when scrolling to the top/right

Introducing the line-ui framework

Here I use the pull-down refresh and The pull-up loading display component is provided by the lin-ui framework. Here I will talk about how to introduce the lin-ui framework:

lin-ui official document address

//在小程序项目目录中执行下面的函数
npm install lin-ui

Then Introduce it in the json file of the page where the component needs to be introduced

"usingComponents": {
    "l-loadmore":"/miniprogram_npm/lin-ui/loadmore/index",
    "l-loading":"/miniprogram_npm/lin-ui/loading/index",
  },

In this way, the lin-ui component is introduced successfully

js code writing

data: {
    downfresh:false,//底部加载展示控制
    upfresh:false//顶部加载展示控制
  },

First set whether to display the loading component in the data. By default, it will not be displayed.

Pull down to refresh js code

//下拉刷新
refresh(){
    if(this.data.upfresh){
      console.log("还没刷新完成")
      return;
    }
    var that = this;
    this.setData({
      upfresh: true,
      // upfresh:false
    })
   
    setTimeout(function() {
      //updateData为自己的数据更新逻辑代码
      that.updateData(true,()=>{
        that.setData({
          upfresh: false,
      });
     })
      // wx.hideLoading();
    console.info(&#39;下拉刷新加载完成.&#39;);
  }, 500);
  },
    
      //更新数据
   updateData:function(tail, callback) {
  
    var that = this;
    console.log("updatedata-=-=seea"+that.data.searchValue)
    wx.request({
      url: app.gBaseUrl + &#39;compony-detail/page&#39;,
      method: &#39;GET&#39;,
      data: {
        page: 0,
        count: 20,
        componyname:that.data.searchValue
      },
      success: (res) => {
        this.setData({
          componys: res.data
        })
        if (callback) {
          callback();
        }
      }
    })
   },

Pull up to load js Code

 /**
   * 滑动到底部加载更多
   */
  getMore(){
    // downloadingData=this.data.downloadingData
    if(this.data.downfresh){
      console.log("还没加载完成")
      return;
    }
   var that = this;
    this.setData({
      downfresh: true,
      // upfresh:false
    })
   
    this.setData({
      downloadingData: true
      // upfresh:false
    })

    setTimeout(function() {
      that.loadData(true,()=>{
        that.setData({
        downfresh: false
      });
     })
      // wx.hideLoading();
    console.info(&#39;上拉数据加载完成.&#39;);
  }, 1000);
  },
    
      loadData: function(tail, callback) {
    var that = this;
    wx.request({
      url: app.gBaseUrl + &#39;compony-detail/page&#39;,
      method: &#39;GET&#39;,
      data: {
        page: that.data.componys.length,
        count: 20,
        componyname:that.data.searchValue
      },
      success: (res) => {
        // console.log(JSON.stringify(res.data))
        that.setData({
          componys: that.data.componys.concat(res.data),
        });
        if (callback) {
          callback();
        }
      }
    })
  },

We have already implemented the entire pull-down refresh and pull-up loading functions. We mainly use the component characteristics of scroll-view to control the display and concealment of the recording component according to the triggering time. Overall It is not difficult to implement, and the specific code can be adjusted appropriately according to your actual situation.

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of A brief discussion on how to implement the pull-down refresh and pull-up loading functions in mini programs? (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:掘金社区. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version