Analysis of encapsulation of scrolling list component in Angular 6
The content shared with you in this article is about the analysis of the encapsulation of the scrolling list component in Angular 6. It has certain reference value. Friends in need can refer to it.
Preface
Learning should be a process of combining input
and output
. This is the reason for writing this article.
In large screen display web APP, scrolling lists are often used. After several attempts, I settled on a pretty good idea.
Requirements
The thead part of the list header is static, while the tbody part scrolls upward.
After the body part is scrolled, the data needs to be refreshed. The final effect is to display all relevant data in the database in an upward scrolling form.
Analysis
If the amount of data is relatively small, we can take out all the data at once and put it into the DOM for circular scrolling. In fact, it is similar to the effect of a carousel.
But if there is a lot of data, doing so may cause a memory leak. Naturally, we can think of paginating the list data. My initial idea is to put a p
as a container on the outer layer of table
, and then table
periodically increases the top
value, etc. table
Halfway through the run, request data from the backend, dynamically create a component tbody
and insert it into table
, and then wait for the previous tbody
When you finish walking (out of sight), delete this component. The idea seemed feasible, but it ran into trouble in practice. When deleting the previous component, the height of table
will be reduced, and the table will instantly fall down. This is obviously not what we want, and the side effects are quite serious.
In this case, I separated tbody
into two table
, and the two table
loops. When there is no data under the previous table
, the second table
starts to walk, and waits for the first table
to completely walk outp
, reset its position to below p
, update the data, and then repeat the actions in between. It’s a little troublesome to complete, but the effect is passable and satisfactory. The problem is that the two timers are unstable. When I open other software and come back, the two tables run inconsistently. For this congenital disease, setInterval
is not accurate enough, and the two timers are prone to poor coordination.
Finally, on the way home from get off work, I thought of a method that didn't require two tables. Use only one table
to move up regularly. When halfway through, clear the timer, reset the position, and update half of the data. That is to say, the first half of the data in the array is removed, and the new data pulled from the background is spliced onto the array. In this way, the data can be continuously refreshed, and table
looks like it keeps going up.
Code
scroll-table.component.html
<p> </p>
字段1 | 字段2 | 字段3 | 字段4 |
---|
{{ele.field01}} | {{ele.field02}} | {{ele.field03}} |
{{ele.field04}} |
scroll-table.component.ts
import { Component, OnInit, ViewChild, ElementRef, Input } from '@angular/core'; import { HttpService } from '../http.service'; @Component({ selector: 'app-scroll-table', templateUrl: './scroll-table.component.html', styleUrls: ['./scroll-table.component.scss'] }) export class ScrollTableComponent implements OnInit { tbody: any = []; @Input() url; //将地址变成组件的一个参数,也就是输入属性 //控制滚动的元素 @ViewChild('scroller') scrollerRef: ElementRef; timer: any; freshData: any; pageNow = 1;//pageNow是当前数据的页码,初始化为1 constructor(private http: HttpService) {} ngOnInit() { //初始化拿到native let scroller: HTMLElement = this.scrollerRef.nativeElement; this.http.sendRequest(this.url).subscribe((data :any[]) => { this.tbody = data.concat(data); }); //开启定时器 this.timer = this.go(scroller); } getFreshData() { //每次请求数据时,pageNow自增1 this.http.sendRequest(`${this.url}?pageNow=${++this.pageNow}`).subscribe((data:any[]) => { if(data.length { let style = document.defaultView.getComputedStyle(scroller, null); let top = parseInt(style.top, 10); if (moved <h3 id="scroll-table-component-scss">scroll-table.component.scss</h3><pre class="brush:php;toolbar:false">.table-container { width: 100%; height: 100%; } .head-show { border-top: 1px solid #4076b9; height: 11.7%; } .scroller-container { border-bottom: 1px solid #4076b9; //border: 1px solid #fff; width: 100%; //height: 88.3%; height: 250px; box-sizing: border-box; overflow: hidden; position:relative; .scroller { position: absolute; top:0; left:0; transition: top .5s ease; } } table { width: 100%; border-collapse: collapse; table-layout: fixed; //border-bottom:1px solid #4076b9; th { border-bottom:1px dashed #2d4f85; color:#10adda; padding:8px 2px; font-size: 14px; } td { border-bottom: 1px dashed #2d4f85; font-size: 12px; color:#10adda; position: relative; height: 49px; p{ padding:0 2px; box-sizing: border-box; text-align:center; display: table-cell; overflow: hidden; vertical-align: middle; } //border-width:1px 0 ; } }
The effect of this is that the component only needs to pass in one parameter url
, and then all operations, including updating data, are completed by the component itself. This completes the component encapsulation and facilitates reuse.
Summary and Thoughts
1. Update data should be updated at the source, that is to say, do not add and delete DOM elements. This operation is troublesome and the performance is low. Putting it at the source means making a fuss about the array that stores the display data in the component class.
2. New data requested in the background should be prepared in advance and placed in another temporary array. It is equivalent to a cache and a temporary register.
3. I imagine the component as a function. It has only one parameter, which is the address of the data. As long as this parameter is present, the component can work normally and does not depend on any other value. Loose coupling.
4. Strengthen the idea of functional programming. Although this is the characteristic of React
, I always feel that angular
can also be used.
Related recommendations:
How to set AngularJs custom directives and the naming convention of custom directives
##In AngularJs What is the relationship between model, Controller and View? (Pictures and text)
The above is the detailed content of Analysis of encapsulation of scrolling list component in Angular 6. For more information, please follow other related articles on the PHP Chinese website!

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
