ashwin-sureshkumar / angular-infinite-scroller Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
your example is scrolling in a div with overflow:scroll that wrap in a container,what if i want to use window scroll to infinite load page , how could do that, thanks in advance :)
May I know the license for this please?
Is it allowed to use this code in other applications?
Hi Ashwin,
I tried to implement this infinite-scroller in a sample project i created but, when i load the page, getStories() function never gets called(no error on console either). Is there anything i am missing?
Thanks,
Shiv
It's a great example!
I don't have time for creating PR, but I think you definitely should update project to RxJs6.
Here is my proposal:
import { Directive, AfterViewInit, ElementRef, Input } from '@angular/core';
import { Observable } from 'rxjs';
import { fromEvent } from 'rxjs';
import { map, pairwise, filter, startWith, exhaustMap } from 'rxjs/operators';
interface ScrollPosition {
sH: number;
sT: number;
cH: number;
}
const DEFAULT_SCROLL_POSITION: ScrollPosition = {
sH: 0,
sT: 0,
cH: 0
};
@Directive({
selector: '[appScroller]'
})
export class ScrollerDirective implements AfterViewInit {
private scrollEvent$: Observable<any>;
private userScrolledDown$: Observable<any>;
private requestStream$: Observable<any>;
private requestOnScroll$: Observable<any>;
@Input()
scrollCallback;
@Input()
immediateCallback;
@Input()
scrollPercent = 70;
constructor(private elm: ElementRef) { }
ngAfterViewInit() {
this.registerScrollEvent();
this.streamScrollEvents();
this.requestCallbackOnScroll();
}
private registerScrollEvent() {
// this.scrollEvent$ = fromEvent(this.elm.nativeElement, 'scroll');
this.scrollEvent$ = fromEvent(window, 'scroll');
}
private streamScrollEvents() {
this.userScrolledDown$ = this.scrollEvent$.pipe(
map((e: any): ScrollPosition => ({
sH: e.target.scrollHeight,
sT: e.target.scrollTop,
cH: e.target.clientHeight
})),
pairwise(),
filter(positions => this.isUserScrollingDown(positions) && this.isScrollExpectedPercent(positions[1])));
}
private requestCallbackOnScroll() {
this.requestOnScroll$ = this.userScrolledDown$;
if (this.immediateCallback) {
this.requestOnScroll$ = this.requestOnScroll$.pipe(
startWith([DEFAULT_SCROLL_POSITION, DEFAULT_SCROLL_POSITION]));
}
this.requestOnScroll$.pipe(
exhaustMap(() => {
return this.scrollCallback();
}))
.subscribe((data) => { console.log(data); }, (err) => console.log(err));
}
private isUserScrollingDown = (positions) => {
return positions[0].sT < positions[1].sT;
}
private isScrollExpectedPercent = (position) => {
return ((position.sT + position.cH) / position.sH) > (this.scrollPercent / 100);
}
}
Can you post a example for angular table with window scroll, or a method to implement for angular table with windows scroll
This is some awesome piece of code and I would like to use it in one of my projects, but however I face some issues when integrating it. What I want is the following:
User has a form where he can enter his search criteria. When he clicks submit, I make a REST API call to fetch the results. The results are transformed into my model class.
By default I only fetch 10 items on every REST API call. So for the first time the user loads these 10 items by clicking the Search button
From next time onwards, I should be able to just scroll down which will in turn load 10 more items from the REST API
I tried to use your scroll directive and I'm stuck with one issue which is in my Typescript which is as below:
import { Component, OnInit } from '@angular/core';
import { PowerPlantService, UserService } from '../shared';
import { User } from '../shared/models/user.model';
import { PowerPlant } from '../shared/models/powerplant.model';
import {PowerPlantSearchParams} from '../shared/models/powerplantsearchparams.model';
@Component({
selector: 'app-home-page',
templateUrl: './home.component.html'
})
export class HomeComponent implements OnInit {
// Represents the PowerPlantTypes
powerPlantTypes = ['RampUpType', 'OnOffType'];
// Represents the status of a PowerPlant
powerPlantStatuses = ['Active & Disabled', 'Active', 'Disabled'];
// Represents the search form
model: any = {};
// represents the list of PowerPlant data
powerPlants: PowerPlant[];
scrollCallback;
currentPage = 1;
constructor(private powerPlantService: PowerPlantService) {
// this.currentUser = JSON.parse(localStorage.getItem('currentUser'));
// Set the initial values for the drop down fields in the UI
this.resetForm();
}
ngOnInit() {}
resetForm() {
this.model.powerPlantOrg = '';
this.model.powerPlantName = '';
this.model.powerPlantType = '';
this.model.powerPlantStatus = '';
}
searchPowerPlants() {
const powerPlantSearchParams = new PowerPlantSearchParams(
this.model.powerPlantType,
this.model.powerPlantOrg,
this.model.powerPlantName,
this.model.powerPlantStatus);
/*
this.powerPlantService.searchPowerPlants(powerPlantSearchParams).subscribe(result => {
this.powerPlants = <PowerPlant[]> result;
}); */
this.scrollCallback = this.searchPowerPlants().bind(this);
return this.powerPlantService.searchPowerPlants(powerPlantSearchParams, this.currentPage)
.do(this.processData);
}
private processData = (newPowerPlants) => {
this.currentPage++;
this.powerPlants = this.powerPlants.concat(newPowerPlants.json());
}
allPowerPlants(onlyActive: boolean = false, page: number = 1): void {
this.powerPlantService.allPowerPlants(onlyActive, page).subscribe(result => {
this.powerPlants = <PowerPlant[]> result;
});
}
}
In the code above line number 50
this.scrollCallback = this.searchPowerPlants().bind(this);
does not compile! It complains that property bind does not exist on type Observable
Any thoughts on how to get this going? Or Do you have any alternative approach that I could try?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.