Skip to content

Instantly share code, notes, and snippets.

@bohoffi
Created December 13, 2016 08:30
Show Gist options
  • Save bohoffi/533dc5c413834820dd7322a2e5147eef to your computer and use it in GitHub Desktop.
Save bohoffi/533dc5c413834820dd7322a2e5147eef to your computer and use it in GitHub Desktop.
Angular 2 paging component utilizing the router
import {Component, Input, OnInit} from '@angular/core';
@Component({
selector: 'paging',
templateUrl: './paging.component.html',
styleUrls: ['./paging.component.css']
})
export class PagingComponent implements OnInit {
@Input()
prevLabel: string;
@Input()
separator: string;
@Input()
nextLabel: string;
private currentPage: number;
private totalPages: number;
private linkPrefix: string;
private prevRouterLink: Array<any> = [];
private nextRouterLink: Array<any> = [];
constructor() {
this.linkPrefix = '/news/page';
}
ngOnInit(): void {
this.currentPage = 1;
this.totalPages = 1;
}
init(currentPage: number, totalPages: number) {
this.currentPage = currentPage;
this.totalPages = totalPages;
this.prevRouterLink = [];
this.nextRouterLink = [];
this.currentPage = !this.currentPage ? 1 : this.currentPage;
this.prevRouterLink.push(this.linkPrefix);
this.prevRouterLink.push(this.currentPage - 1);
this.nextRouterLink.push(this.linkPrefix);
this.nextRouterLink.push(this.currentPage + 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment