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

Summary of some situations when Docker container disk is full

Preface This article describes two situations I h...

Detailed explanation of mysql scheduled tasks (event events)

1. Brief introduction of the event An event is a ...

Detailed explanation of the usage of compose function and pipe function in JS

Table of contents compose function Array.prototyp...

Tutorial on using Webpack in JavaScript

Table of contents 0. What is Webpack 1. Use of We...

How to call a piece of HTML code together on multiple HTML pages

Method 1: Use script method: Create a common head...

Detailed explanation of Nginx proxy_redirect usage

Today, I encountered a little problem when I was ...

Vue+echarts realizes stacked bar chart

This article shares the specific code of Vue+echa...

Comprehensive understanding of HTML basic structure

Introduction to HTML HyperText Markup Language: H...

MySQL 8.0 Window Function Introduction and Summary

Preface Before MySQL 8.0, it was quite painful to...

14 techniques for high-performance websites

Original : http://developer.yahoo.com/performance...

Detailed Introduction to the MySQL Keyword Distinct

Introduction to the usage of MySQL keyword Distin...

MySQL database implements OLTP benchmark test based on sysbench

Sysbench is an excellent benchmark tool that can ...

HTML table_Powernode Java Academy

To draw a table in HTML, use the table tag tr me...