-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpagination.component.ts
72 lines (62 loc) · 2.59 KB
/
pagination.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { CommonModule } from '@angular/common';
import { Component, EventEmitter, Input, OnChanges, Output } from '@angular/core';
import { FormControl } from '@angular/forms';
import { PaginationParameters, PaginationResources } from './pagination.model'
@Component({
selector: 'pagination',
templateUrl: './pagination.component.html'
})
export class PaginationComponent implements OnChanges {
@Input() currentPage: number = 1;
@Input() totalResultsCount: number;
@Input() pageSize: number = 10;
@Input() pageRange: number = 5;
@Input() pageSizeChoices: Array<number> = this.paginationParameters.pageSizeChoices;
@Output() pageSizeChoiceChange: EventEmitter<number> = new EventEmitter();
@Output() selectedPageChanged: EventEmitter<number> = new EventEmitter();
public totalPageCount: number;
public isPaginationEnabled: boolean = true;
public initialRangeValue: number = 5;
public resources = PaginationResources;
public paginationRange: Array<number> = [];
public lastRangeValue: number;
public viewSize: FormControl = new FormControl();
public pageSizeChoiceChanged: boolean = false;
private componentName: string = 'PaginationComponent';
constructor(private paginationParameters: PaginationParameters) {
}
ngOnChanges() {
this.InitializePaginationComponent();
}
public setPageNumber(pageNumber: number): void {
if (this.currentPage !== pageNumber) {
this.currentPage = pageNumber;
this.setPaginationRange();
this.selectedPageChanged.emit(pageNumber);
}
}
public setPaginationRange() {
const initialRangeLimit = Math.max(this.totalPageCount - this.pageRange + 1, 1);
this.initialRangeValue = (this.currentPage <= 5) ? 1 : Math.min(this.currentPage - 2, initialRangeLimit);
this.paginationRange = [];
for (let i = 0; i < this.pageRange; i++) {
const pageNumber = this.initialRangeValue + i;
if (pageNumber > this.totalPageCount) {
break;
}
this.lastRangeValue = pageNumber;
this.paginationRange.push(pageNumber);
}
}
private InitializePaginationComponent() {
this.pageSizeChoiceChanged = false;
this.totalPageCount = Math.ceil(this.totalResultsCount / this.pageSize);
this.setPaginationRange();
this.viewSize.setValue(this.pageSize);
}
public OnPageSizeChoiceChange(value: number) {
this.pageSizeChoiceChanged = true;
this.pageSize = value;
this.pageSizeChoiceChange.emit(value);
}
}