1- import { Component , ElementRef , Injectable } from ' @angular/core' ;
2- import { Observable , of } from ' rxjs' ;
3- import { delay , map } from ' rxjs/operators' ;
4- import { CorporateEmployee } from ' ./model/corporate-employee' ;
1+ import { Component , ElementRef , Injectable } from " @angular/core" ;
2+ import { Observable , of } from " rxjs" ;
3+ import { delay , map } from " rxjs/operators" ;
4+ import { CorporateEmployee } from " ./model/corporate-employee" ;
55
6- const companyData = require ( ' ../../assets/data/company.json' ) ;
6+ const companyData = require ( " ../../assets/data/company.json" ) ;
77
88class PagedData < T > {
99 data : T [ ] ;
@@ -14,18 +14,20 @@ class PagedData<T> {
1414 */
1515@Injectable ( )
1616export class MockServerResultsService {
17-
18- public getResults ( offset : number , limit : number ) : Observable < PagedData < CorporateEmployee > > {
19- return of ( companyData . slice ( offset , offset + limit ) )
20- . pipe ( delay ( new Date ( Date . now ( ) + 500 ) ) , map ( data => ( { data } ) ) ) ;
17+ public getResults (
18+ offset : number ,
19+ limit : number
20+ ) : Observable < PagedData < CorporateEmployee > > {
21+ return of ( companyData . slice ( offset , offset + limit ) ) . pipe (
22+ delay ( new Date ( Date . now ( ) + 500 ) ) ,
23+ map ( data => ( { data } ) )
24+ ) ;
2125 }
2226}
2327
2428@Component ( {
25- selector : 'server-scrolling-demo' ,
26- providers : [
27- MockServerResultsService
28- ] ,
29+ selector : "server-scrolling-demo" ,
30+ providers : [ MockServerResultsService ] ,
2931 template : `
3032 <div>
3133 <h3>
@@ -49,36 +51,40 @@ export class MockServerResultsService {
4951 ></ngx-datatable>
5052 </div>
5153 ` ,
52- styleUrls : [ ' ./scrolling-server.component.css' ] ,
54+ styleUrls : [ " ./scrolling-server.component.css" ]
5355} )
5456export class ServerScrollingComponent {
55-
5657 readonly headerHeight = 50 ;
5758 readonly rowHeight = 50 ;
5859 readonly pageLimit = 10 ;
5960
6061 rows : CorporateEmployee [ ] = [ ] ;
6162 isLoading : boolean ;
6263
63- constructor ( private serverResultsService : MockServerResultsService , private el : ElementRef ) { }
64+ constructor (
65+ private serverResultsService : MockServerResultsService ,
66+ private el : ElementRef
67+ ) { }
6468
6569 ngOnInit ( ) {
6670 this . onScroll ( 0 ) ;
6771 }
6872
6973 onScroll ( offsetY : number ) {
7074 // total height of all rows in the viewport
71- const viewHeight = this . el . nativeElement . getBoundingClientRect ( ) . height - this . headerHeight ;
75+ const viewHeight =
76+ this . el . nativeElement . getBoundingClientRect ( ) . height - this . headerHeight ;
7277
7378 // check if we scrolled to the end of the viewport
74- if ( ! this . isLoading && offsetY + viewHeight >= this . rows . length * this . rowHeight ) {
75-
79+ if (
80+ ! this . isLoading &&
81+ offsetY + viewHeight >= this . rows . length * this . rowHeight
82+ ) {
7683 // total number of results to load
7784 let limit = this . pageLimit ;
7885
7986 // check if we haven't fetched any results yet
8087 if ( this . rows . length === 0 ) {
81-
8288 // calculate the number of rows that fit within viewport
8389 const pageSize = Math . ceil ( viewHeight / this . rowHeight ) ;
8490
@@ -95,10 +101,12 @@ export class ServerScrollingComponent {
95101 // 1) it prevents the same page from being loaded twice
96102 // 2) it enables display of the loading indicator
97103 this . isLoading = true ;
98-
99- this . serverResultsService . getResults ( this . rows . length , limit ) . subscribe ( results => {
100- this . rows . push ( ...results . data ) ;
101- this . isLoading = false ;
102- } ) ;
104+
105+ this . serverResultsService
106+ . getResults ( this . rows . length , limit )
107+ . subscribe ( results => {
108+ this . rows = [ ...this . rows , ...results . data ] ;
109+ this . isLoading = false ;
110+ } ) ;
103111 }
104112}
0 commit comments