33
loading...
This website collects cookies to deliver better user experience
ng new ngxInfiniteScrollerDemo
ng generate component hello
ng generate component scrollContainer
npm install ngx-infinite-scroll
npm install @angular/cdk
<app-scroll-container>
<mat-table>
<ng-container>
<mat-header-cell>
{{ column | titlecase }}
</mat-header-cell>
<mat-cell>{{ item[column] }}</mat-cell>
</ng-container>
<mat-header-row></mat-header-row>
<mat-row></mat-row>
</mat-table>
</app-scroll-container>
:host {
justify-content: flex-start;
display: flex;
flex-direction: column;
}
app-scroll-container {
flex-grow: 0;
flex-shrink: 0;
}
app-scroll-container.full {
flex-basis: auto;
}
app-scroll-container.part {
flex-basis: 200px;
}
.buttons {
position: absolute;
top: 0;
right: 0;
z-index: 1000;
}
import { Component, OnInit, ViewChild } from '@angular/core';
import { Sort, MatSort, MatTableDataSource } from '@angular/material';
import { noop as _noop } from 'lodash-es';
interface Element {
name: string;
serial_no: number;
weight: number;
height: number;
}
const ELEMENT_DATA: Element[] = [
{serial_no: 1, name: 'Bunty', weight: 50, height: 5.1},
{serial_no: 2, name: 'Gaurav', weight: 52, height: 5.2},
{serial_no: 3, name: 'Jayraj', weight: 69, height: 5.5},
{serial_no: 4, name: 'Lokesh', weight: 90, height: 6},
{serial_no: 5, name: 'Hardik', weight: 41, height: 5},
{serial_no: 6, name: 'Sunil', weight: 27, height: 6},
{serial_no: 7, name: 'Aniket', weight: 45, height: 4.5},
{serial_no: 8, name: 'Jignesh', weight: 94, height: 6},
{serial_no: 9, name: 'Abdul', weight: 84, height: 5},
{serial_no: 10, name: 'James', weight: 29, height: 4},
{serial_no: 11, name: 'Jigar', weight: 56, height: 5.1},
{serial_no: 12, name: 'Jaimin', weight: 35, height: 5},
{serial_no: 13, name: 'Nitin', weight: 68, height: 5.5},
{serial_no: 14, name: 'Suhani', weight: 85, height: 3.5},
{serial_no: 16, name: 'Lalit', weight: 36, height: 4},
{serial_no: 17, name: 'Hiren', weight: 53, height: 4.5},
{serial_no: 18, name: 'Himanshu', weight: 48, height: 4},
{serial_no: 19, name: 'Piyush', weight: 93, height: 5},
{serial_no: 20, name: 'Keval', weight: 78, height: 5.2},
];
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
dataSource: MatTableDataSource<element>;
limit: number = 1000;
displayedColumns: string[] = ['serial_no', 'name', 'weight', 'height'];
full: boolean = true;
@ViewChild(MatSort) sort: MatSort;
constructor() { }
ngOnInit() {
this.getData();
}
handleScroll = (scrolled: boolean) => {
console.timeEnd('lastScrolled');
scrolled ? this.getData() : _noop();
console.time('lastScrolled');
}
hasMore = () => !this.dataSource || this.dataSource.data.length < this.limit;
getData() {
const data: Element[] = this.dataSource
? [...this.dataSource.data, ...ELEMENT_DATA]
: ELEMENT_DATA;
this.dataSource = new MatTableDataSource(data);
this.dataSource.sort = this.sort;
}
}
</element>
import { Component, OnInit, OnChanges, Input, Output, EventEmitter, HostListener, ElementRef } from '@angular/core';
import { throttle as _throttle, noop as _noop } from "lodash-es";
enum ScrollDirection {
UP = 'up',
DOWN = 'down'
}
enum ScrollListener {
HOST = 'scroll',
WINDOW = 'window:scroll'
}
@Component({
selector: 'app-scroll-container',
templateUrl: './scroll-container.component.html',
styleUrls: ['./scroll-container.component.css']
})
export class ScrollContainerComponent implements OnInit, OnChanges {
private _element: Element;
private _window: Element;
public scrollTop = 0;
@Input() more = true;
@Input() scrollDelay = 500;
@Input() scrollOffset = 1000;
@Output() scrolled: EventEmitter<boolean> = new EventEmitter<boolean>();
@HostListener(ScrollListener.HOST) _scroll: Function;
@HostListener(ScrollListener.WINDOW) _windowScroll: Function;
constructor(private elRef: ElementRef) {
this._element = this.elRef.nativeElement;
this._window = document.documentElement as Element;
}
ngOnInit() {
this.setThrottle();
}
ngOnChanges(changes) {
if (changes.scrollDelay) this.setThrottle();
}
setThrottle() {
this._scroll = this._windowScroll = _throttle(this.handleScroll, this.scrollDelay);
}
getListener = () => this.elRef.nativeElement.clientHeight === this.elRef.nativeElement.scrollHeight
? ScrollListener.WINDOW
: ScrollListener.HOST
roundTo = (from: number, to: number = this.scrollOffset) => Math.floor(from / to) * to;
getScrollDirection = (st: number) => this.scrollTop <= st ? ScrollDirection.DOWN : ScrollDirection.UP;
canScroll(e: Element): boolean {
const scrolled = this.more
&& this.getScrollDirection(e.scrollTop) === ScrollDirection.DOWN
&& this.roundTo(e.clientHeight) === this.roundTo(e.scrollHeight - e.scrollTop);
this.scrollTop = e.scrollTop;
return scrolled;
}
handleScroll = () => this.getListener() === ScrollListener.HOST
? this.scrolled.emit( this.canScroll(this._element) )
: this.scrolled.emit( this.canScroll(this._window) )
}
</boolean></boolean>
<ng-content></ng-content>
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatTableModule, MatSortModule, MatProgressSpinnerModule } from '@angular/material';
import { AppComponent } from './app.component';
import { HelloComponent } from './hello.component';
import { ScrollContainerComponent } from './scroll-container/scroll-container.component';
@NgModule({
imports: [ BrowserModule, FormsModule, BrowserAnimationsModule, MatTableModule, MatSortModule, MatProgressSpinnerModule ],
declarations: [ AppComponent, HelloComponent, ScrollContainerComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }