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
Preface This article describes two situations I h...
When you learn MySQL, you will find that it comes...
1. Brief introduction of the event An event is a ...
Table of contents compose function Array.prototyp...
Table of contents 0. What is Webpack 1. Use of We...
Method 1: Use script method: Create a common head...
Today, I encountered a little problem when I was ...
This article shares the specific code of Vue+echa...
Introduction to HTML HyperText Markup Language: H...
Preface Before MySQL 8.0, it was quite painful to...
Original : http://developer.yahoo.com/performance...
Ubuntu16.04 install and uninstall pip Experimenta...
Introduction to the usage of MySQL keyword Distin...
Sysbench is an excellent benchmark tool that can ...
To draw a table in HTML, use the table tag tr me...