Vue implements an example of pulling down and scrolling to load data

Vue implements an example of pulling down and scrolling to load data

Web projects often use the function of pulling down and scrolling to load data. Today, I will introduce the Vue-infinite-loading plug-in and explain how to use it!

Step 1: Installation

npm install vue-infinite-loading --save

Step 2: Citation

import InfiniteLoading from 'vue-infinite-loading';

export default {
  components: { InfiniteLoading }
}

Step 3: Use

1. Basic usage

<template>
  <div> <p v-for="item in list"> <span v-text="item"></span> </p> <!--The infinite-loading component should be placed at the bottom of the list, inside the scrolling box! --> <infinite-loading @infinite="infiniteHandler"></infinite-loading> </div></template><script> import InfiniteLoading from 'vue-infinite-loading';

  export default {
    data() {
      return {
        list: []
      };
    },
    methods: {
      infiniteHandler($state) {
        // Here simulates a loading delay of 1 second setTimeout(() => {
          let temp = [];
          for (let i = this.list.length + 1; i <= this.list.length + 20; i++) {
            temp.push(i);
          }
          this.list = this.list.concat(temp);
          $state.loaded();
        }, 1000);
      },
    },
    components: { InfiniteLoading }
  }</script>

2. Paging usage

<template>
    <div>
          <ul>
              <li class="hacker-news-item" v-for="(item, key) in list"></li>
        </ul>
        <infinite-loading @infinite="infiniteHandler">
            No more Data
        </infinite-loading>
    </div>
</template>    

<script>
    import InfiniteLoading from 'vue-infinite-loading';
    import axios from 'axios';

    export default {
          data() {
            return {
                  list: []
            };
          },
          methods: {
            infiniteHandler($state) {
                let api="http://xxx.com/xxx";
                //api requests data address for you axios.get(api, {
                    params: {
                        // Page number parameters (10 per page)
                          page: this.list.length / 10 + 1,
                    },
                  }).then((response) => {
                    if (response.data.length) {
                        // response.data is the array list returned by your request interface this.list = this.list.concat(response.data);
                          $state.loaded();
                          if (this.list.length / 10 === 10) {
                              // Here 10 pages of data are loaded, and the setting is not to load anymore $state.complete(); 
                          }
                    } else {
                          $state.complete();
                    }
                  });
            }
          },
          components: { InfiniteLoading }
    };
</script>

Description: $state: This component will pass a special event parameter $state to the event handler to change the loading state. The $state parameter includes three methods, namely loaded method, complete method and reset method.

  • The loaded method is used to stop the animation after each data load, and then the component will be ready for the next trigger.
  • The complete method is used to complete the infinite loading, and the component will no longer process any scrolling operations. If the method is never called when loaded calls the complete method, this component will display the result message for the user, if not, it will display no more user messages, and other content can be set by slot
  • The reset method returns the component to its original state

3. Conditional Usage

<template>
    <div class="hacker-news-list">
          <div class="hacker-news-header">
            <!--Pull down to change-->
            <select v-model="tag" @change="changeFilter()">
                  <option value="story">Story</option>
                  <option value="history">History</option>
            </select>
              <!--Or click to change-->
            <button @click="changeFilter()">Search</button>
          </div>
          <ul>
              <li class="hacker-news-item" v-for="(item, key) in list"></li>
           </ul>
           <!--Don't forget to set this ref="infiniteLoading"-->
          <infinite-loading @infinite="infiniteHandler" ref="infiniteLoading">
            No more data
          </infinite-loading>
    </div>
</template>

<script>
    import InfiniteLoading from 'vue-infinite-loading';
    import axios from 'axios';

    export default {
          data() {
            return {
                  list: [],
                  tag: 'story',
            };
          },
          methods: {
            infiniteHandler($state) {
                  const api="http://xxx.com/xxx";
                  //api requests data address for you axios.get(api, {   
                    params: {
                        // Changed conditional parameters tags: this.tag,  
                          page: this.list.length / 10 + 1,
                    },
                  }).then(({ data }) => {
                    if (data.result.length) {
                          this.list = this.list.concat(data.result);
                          $state.loaded();
                          if (this.list.length / 20 === 10) {
                            state.complete();
                          }
                    } else {
                          $state.complete();
                    }
                  });
            },
            //Change the conditional bar using this method changeFilter() {
                  this.list = [];
                  this.$nextTick(() => {
                    this.$refs.infiniteLoading.$emit('$InfiniteLoading:reset');
                  });
            },
          },
          components: { InfiniteLoading }
    }
</script>

Official link: https://peachscript.github.io/vue-infinite-loading/

GitHub link: https://github.com/PeachScript/vue-infinite-loading

The above is the details of the example of Vue's drop-down scrolling to load data. For more information about Vue's drop-down scrolling to load data, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Vue remembers the scroll bar and the perfect way to implement drop-down loading
  • Vue implements alphabetical sorting of singer list, drop-down scroll bar, and real-time update of sidebar sorting
  • Vue monitors the method of pulling a div vertical scroll bar down to the bottom
  • Vue uses mint-ui to implement pull-down refresh and infinite scrolling sample code
  • Vue implements ajax scrolling and pull-down loading, and also has loading effect (recommended)
  • Pull-down refresh instructions and scroll refresh instructions based on Vue
  • Vue mobile pull down to refresh and slide up to load
  • Vue implements network picture waterfall flow + pull down to refresh + pull up to load more (detailed steps)
  • Vue's pull-down loading is actually not that complicated
  • VueScroll implements pull-down refresh and pull-up loading on mobile devices
  • Vue plugin mescroll.js implements pull-up loading and pull-down refresh on mobile devices
  • How to encapsulate a component for pulling down to load the next page of data on a Vue mobile terminal

<<:  Examples of using the or statement in MySQL

>>:  VMware Workstation 14 Pro installation and activation graphic tutorial

Recommend

Methods and steps to access Baidu Maps API with JavaScript

Table of contents 1. Baidu Map API Access 2. Usin...

Understanding and usage scenarios of ES6 extension operators

Table of contents 1. Replace the apply method, ge...

MySQL subqueries and grouped queries

Table of contents Overview Subqueries Subquery Cl...

Node.js+postman to simulate HTTP server and client interaction

Table of contents 1. Node builds HTTP server 2. H...

How to write the Nofollow tag and how to use it

The "nofollow" tag was proposed by Goog...

Tutorial on installing MySQL 8.0.11 using RPM on Linux (CentOS7)

Table of contents 1. Installation preparation 1. ...

Correct modification steps for Docker's default network segment

background A colleague is working on his security...

Detailed explanation of this pointing in JS arrow function

Arrow function is a new feature in ES6. It does n...

MySQL 5.7.25 installation and configuration method graphic tutorial

There are two types of MySQL installation files, ...

Use Vue3 to implement a component that can be called with js

Table of contents Preface 1. Conventional Vue com...

Ubuntu basic settings: installation and use of openssh-server

Record the installation and use of openssh-server...

Detailed tutorial on installing MySQL 8 in CentOS 7

Prepare Environmental information for this articl...

Vue parent component calls child component function implementation

Vue parent component calls the function of the ch...

Tutorial on how to remotely connect to MySQL database under Linux system

Preface I recently encountered this requirement a...