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 methodThe 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:
|
<<: Docker+daocloud realizes automatic construction and deployment of front-end projects
>>: Summary of commonly used time, date and conversion functions in Mysql
Table of contents in conclusion Practice Analysis...
Article mind map Why use master-slave replication...
Because we were going to build a website, in addi...
1.Jenkins installation steps: https://www.jb51.ne...
Preface Recently our server was attacked by hacke...
Table of contents Preface 1. typeof 2. instanceof...
Table of contents 1. Functional description 2. Pa...
Achieve resultsImplementation Code html <base ...
This article example shares the specific code of ...
Sometimes you need to set several areas on a pict...
Demand: This demand is an urgent need! In a subwa...
1. Application of multimedia in HTML_falsh animat...
The css technique for changing the color of an im...
ffmpeg is a very powerful audio and video process...
Box-sizing in CSS3 (content-box and border-box) T...