Preface We often use tables. If the amount of data is large, we can directly paginate it, but it is inevitable that there will be some situations where many items need to be displayed on one page, or there is no need for pagination. At this time, it would be fine if it is just for display, but if the list is editable, it will become very stuck. When you click on the drop-down box, it will take a long time to pop up. This greatly affects user usage. Solution
Specific implementation When loading, get 10/20 pieces of data (this can be calculated by the height of each row/the height of the page display data, or a fixed value), monitor the scroll bar when scrolling, and calculate the data of the display page based on the scrolling distance. Prerequisites to be met The total height of the list
If the page is only as high as it is displayed, you cannot scroll to get new data. The height of each row
Scroll offset
The starting index and ending index of the data displayed on the page
Code Steps <div class="main-inner-content"> <el-table :data="visibleData" :style="{'min-height': gradingEditor ? listHeight+'px':'100%'}" id="dataTable"> </el-table> </div> computed: { // //Total list height listHeight () { // tableData is the total data of the interface return this.tableData.length * this.itemSize; }, // //The style corresponding to the offset getTransform () { return `translate3d(0,${this.startOffset}px,0)`; }, //Get the actual display list data visibleData () { let tableBody = document.querySelector('#dataTable >.el-table__body-wrapper') if (tableBody) { tableBody.style.transform = this.getTransform } return this.tableData.slice(this.start, Math.min(this.end, this.tableData.length)); } }, data() { return { tableData:[], //Offset startOffset: 0, //Starting index start: 0, //End index end: null, }; }, methods:{ handleScroll () { // This is a scrolling box. If the scrolling position is a table, just change it accordingly. There is also a place to assign an offset. let scrollTop = document.getElementById('main-inner-content').scrollTop; // //The starting index at this time this.start = Math.floor(scrollTop / this.itemSize); if (this.start < 0) this.start = 0; // //The end index at this time this.end = this.start + 10; // //The offset at this time this.startOffset = scrollTop - (scrollTop % this.itemSize); this.startOffset = this.startOffset > this.itemSize ? this.startOffset - this.itemSize : 0; } }, mounted(){ window.addEventListener("scroll", this.handleScroll, true); }, destroyed() window.removeEventListener('scroll', this.handleScroll, true) // Clear scroll bar scroll events} When the page scrolls, only ten pieces of data will be loaded and the page display will be determined by the offset. questionI set the total height for the entire el-table, and then set the offset for the box of the list item below. If the page needs to re-acquire data without refreshing (such as paging), the data must be initialized, otherwise the page will directly display the previous data or the page will be blank. document.querySelector('#main-inner-content').scrollTop = 0 this.start = 0 this.startOffset = 0 this.end = this.start + 10; let tableBody = document.querySelector('#dataTable >.el-table__body-wrapper') tableBody.style.transform = this.getTransform For the convenience of calculation and use, I use a fixed height for each line of the page and use the excess omission method. However, after deploying the pre-release environment, you will find that the code -webkit-box-orient: vertical is directly gone and not displayed. .omit-text { word-wrap: break-word; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; } refer to This concludes this article on using virtual lists in el-table to optimize tables. For more el-table optimization content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Mysql database design three paradigm examples analysis
>>: Docker private warehouse harbor construction process
When searching online for methods to install MySQ...
Configuration file that needs to be loaded when t...
This article shares with you the installation and...
describe: When the Tabs component switches back a...
The following error message appears when installi...
Table of contents 1 Introduction 2 Trigger Introd...
Step 1: Ensure that MySQL has binlog enabled show...
Normal explanation % means any client can connect...
Table of contents Single Node Diff reconcileSingl...
After entering the Docker container, if you exit ...
ARGB is a color mode, which is the RGB color mode...
Sometimes it is slow to download large network fi...
Table of contents 1. Test environment 1.1 Hardwar...
I have been studying the source code of Vue recen...
Problem Description The button style is icon + te...