Vue implements pull-down to load more

Vue implements pull-down to load more

Developers familiar with Element-UI may have had this experience: its infinite scrolling InfiniteScroll is not easy to use. Here are two ways to implement pull-down loading:

1. Use el-table-infinite-scroll plugin

(1). Install the plugin

npm install --save el-table-infinite-scroll

(2). Global import and registration

// main.js
 
import elTableInfiniteScroll from 'el-table-infinite-scroll';
 
Vue.use(elTableInfiniteScroll);

(3) Local file import

<script>
// Import import elTableInfiniteScroll from 'el-table-infinite-scroll';
export default {
  // Register directives: {
    'el-table-infinite-scroll': elTableInfiniteScroll
  }
}
</script>

(4). Use instructions

<el-table :height="tableHeight" v-el-table-infinite-scroll="loadMore">
 
</el-table>

(5). Code Examples

<template>
    <div class="app-container">
        <el-table :height="tableHeight" v-el-table-infinite-scroll="loadMore" :data="tableList">
            <!-- Table data omitted-->
        </el-table>
    </div>
</template>
 
<script>
// Import the plugin import elTableInfiniteScroll from "el-table-infinite-scroll";
 
export default {
    name: "index",
    data() {
        return {
            // Table height tableHeight:"",
            // Total number of data tableCount:0,
            // Table data list tableList:[],
            // Is it loading? tableLoading: false,
            // Table search conditions tableSearch:{
                page:1
            }
        }
    },
    // Register directives: {
        "el-table-infinite-scroll": elTableInfiniteScroll,
    },
 
    created() {
        let windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
        // Dynamically calculate the height of the table. 200 is the height of other elements on the screen except the table. It depends on the actual situation this.tableHeight = windowHeight - 200 + "px";
    },
    mounted(){
        this.getTableData(this.tableSearch);
    },
 
    methods: {
        // Request table data getTableData(search) {
            let page = search.page;
            let url = "index?page=" + page;
            // The first time you open the page, you need to load the data once. To prevent too much data from automatically triggering scrolling, you need to set it to loading this.tableLoading = true;
            this.$http.get(url).then((result) => {
                if (res.code == 10000) {
                    // Concatenate data this.tableList = this.tableList.concat(result.data.list);
                    this.tableCount = result.count;
                    this.tableSearch.page = result.current;
                    this.tableLoading = false;
                }
            });
        },
        // Load more loadMore() {
            if (!this.tableLoading) {
                this.tableLoading = true;
                if (this.tableList.length < this.tableCount) {
                    this.tableSearch.page++;
                    this.getTableData(this.tableSearch);
                } else {
                    this.$message("All data has been loaded!");
                    this.tableLoading = false;
                }
            }
        },
    }
};
</script>

2. Customize the drop-down loading method

The plug-in used above needs to rely on Element-UI. If Element-UI is not used, you can only write a drop-down load yourself. The implementation code is as follows:

<template>
    <div class="app-container">
        <div :style="{height:tableHeight,overflow:'auto'}" id="tableBox">
            <!-- Table data omitted-->
        </div>
    </div>
</template>
 
<script>
export default {
    name: "index",
    data() {
        return {
            // Table height tableHeight:"",
            // Total number of data tableCount:0,
            // Table data list tableList:[],
            // Is it loading? tableLoading: false,
            // Table search conditions tableSearch:{
                page:1
            }
        };
    },
    created(){
        let windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
        // Dynamically calculate the height of the table. 200 is the height of other elements on the screen except the table. It depends on the actual situation. this.tableHeight = windowHeight - 200 + 'px';
    },
    mounted() {
        this.getTableData(this.tableSearch);
        document.getElementById("tableBox").addEventListener('scroll',this.onTableScroll); 
    },
 
    beforeDestroy() {
        // Remove the listening event window.removeEventListener('scroll', this.onTableScroll)
    },
 
    methods: {
        onTableScroll(){
            var obj = document.getElementById("tableBox");
            var scrollHeight = obj.scrollHeight;
            var scrollTop = obj.scrollTop; 
            var objHeight = obj.offsetHeight;  
            // 100 is the threshold value, which can be adjusted according to the actual situation if(scrollHeight <= (scrollTop + objHeight + 100) && !this.tableLoading && this.tableList.length<this.tableCount){ 
                this.tableLoading = true;
                this.tableSearch.page++;
                setTimeout(()=>{   
                    this.getTableData(this.tableSearch);
                },300)
            }
        },
 
        getTableData(search){
            let page = search.page;
            let url = "index?page=" + page;
            // The first time you open the page, you need to load the data once. To prevent too much data from automatically triggering scrolling, you need to set it to loading this.tableLoading = true;
            this.$http.get(url).then((result) => {
                if (res.code == 10000) {
                    // Concatenate data this.tableList = this.tableList.concat(result.data.list);
                    this.tableCount = result.count;
                    this.tableSearch.page = result.current;
                    this.tableLoading = false;
                }
            });
        },
        
     
    },
};
</script>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Pull-up loading and pull-down refreshing in vue.js mobile app
  • Vue.js integrates vux's pull-up loading and pull-down refreshing example tutorial
  • Vue implements ajax scrolling and pull-down loading, and also has loading effect (recommended)
  • Solve the conflict problem when Vue uses mint-ui loadmore to implement pull-up loading and pull-down refresh, and a page uses multiple pull-up loads
  • Vue2.0 mobile terminal implements pull-down refresh and pull-up load more examples
  • 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
  • Vue uses better-scroll to achieve pull-down refresh and pull-up loading
  • Vue's pull-down loading is actually not that complicated

<<:  Docker+daocloud realizes automatic construction and deployment of front-end projects

>>:  Summary of commonly used time, date and conversion functions in Mysql

Recommend

Analysis of the difference between emits and attrs in Vue3

Table of contents in conclusion Practice Analysis...

Detailed explanation of MySQL master-slave replication and read-write separation

Article mind map Why use master-slave replication...

How to forget the password of Jenkins in Linux

1.Jenkins installation steps: https://www.jb51.ne...

Solutions to Files/Folders That Cannot Be Deleted in Linux

Preface Recently our server was attacked by hacke...

Four methods of using JS to determine data types

Table of contents Preface 1. typeof 2. instanceof...

$nextTick explanation that you can understand at a glance

Table of contents 1. Functional description 2. Pa...

Image scrolling effect made with CSS3

Achieve resultsImplementation Code html <base ...

Vue uses drag and drop to create a structure tree

This article example shares the specific code of ...

Example code for setting hot links and coordinate values ​​for web images

Sometimes you need to set several areas on a pict...

Detailed process of drawing three-dimensional arrow lines using three.js

Demand: This demand is an urgent need! In a subwa...

HTML multimedia application: inserting flash animation and music into web pages

1. Application of multimedia in HTML_falsh animat...

Pure CSS to change the color of the picture

The css technique for changing the color of an im...

Centos7 installation of FFmpeg audio/video tool simple document

ffmpeg is a very powerful audio and video process...

Detailed explanation of box-sizing in CSS3 (content-box and border-box)

Box-sizing in CSS3 (content-box and border-box) T...