Skip to content

Commit 76a5c85

Browse files
authored
Merge pull request #1 from khalid-folio3/master
Adding ellipses in page numbers
2 parents 2a6edf5 + fbc44bf commit 76a5c85

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ add in .angular-cli.json
3737
and for prodaction build use
3838

3939
`ng build --prod --aot=false`
40-
40+
41+
### Added numerical pagination support with 2 next and previous links
42+
4143
#### Licensing
4244
MIT License

src/components/pagination.component.ts

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import { Component, Inject, forwardRef, Input } from '@angular/core';
2-
import { DataTable } from './table.component';
3-
import { PAGINATION_TEMPLATE } from './pagination.template';
4-
import { PAGINATION_STYLE } from "./pagination.style";
1+
import {Component, Inject, forwardRef, Input} from '@angular/core';
2+
import {DataTable} from './table.component';
3+
import {PAGINATION_TEMPLATE} from './pagination.template';
4+
import {PAGINATION_STYLE} from "./pagination.style";
55

66

77
@Component({
88
moduleId: module.id,
9-
selector: 'data-table-pagination',
10-
template: PAGINATION_TEMPLATE,
11-
styles: [PAGINATION_STYLE]
9+
selector: 'data-table-pagination',
10+
template: PAGINATION_TEMPLATE,
11+
styles: [PAGINATION_STYLE]
1212
})
1313
export class DataTablePagination {
1414

@@ -17,7 +17,8 @@ export class DataTablePagination {
1717
@Input() show_input = false;
1818
@Input() show_numbers = true;
1919

20-
constructor(@Inject(forwardRef(() => DataTable)) public dataTable: DataTable) {}
20+
constructor(@Inject(forwardRef(() => DataTable)) public dataTable: DataTable) {
21+
}
2122

2223
pageBack() {
2324
this.dataTable.offset -= Math.min(this.dataTable.limit, this.dataTable.offset);
@@ -55,10 +56,40 @@ export class DataTablePagination {
5556
this.dataTable.page = Number(<any>value);
5657
}
5758

58-
createPageRange(number): any[] {
59+
hasPrevious(number, page) {
60+
const difference = this.getDifference();
61+
if ((page - difference) > 1) {
62+
return true;
63+
}
64+
return false;
65+
}
66+
67+
hasNext(number, page) {
68+
const difference = this.getDifference();
69+
if ((number - page) > difference) {
70+
return true;
71+
}
72+
return false;
73+
}
74+
75+
getDifference() {
76+
const difference = 2;
77+
return difference;
78+
}
79+
80+
createPageRange(number, page): any[] {
5981
let items: number[] = [];
6082
if (number > 1) {
61-
for (let i = 1; i <= number; i++) {
83+
const difference = this.getDifference();
84+
let maxPage = number;
85+
let minPage = 1;
86+
if ((number - page) >= difference) {
87+
maxPage = page + difference;
88+
}
89+
if ((page - difference) >= 1) {
90+
minPage = page - difference;
91+
}
92+
for (let i = minPage; i <= maxPage; i++) {
6293
items.push(i);
6394
}
6495
}

src/components/pagination.template.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ export const PAGINATION_TEMPLATE = `
3131
</div>
3232
</div>
3333
</div>
34+
<button *ngIf="hasPrevious(maxPage,page)" [disabled]="true" (click)="false" class="btn btn-default hasPrevious">...</button>
3435
<div class="pagination-page" *ngIf="show_numbers">
35-
<button *ngFor="let i of createPageRange(maxPage)"
36+
<button *ngFor="let i of createPageRange(maxPage,page)"
3637
[disabled]="i == page"
3738
(click)="page = i"
3839
class="btn btn-default">{{ i }}</button>
3940
</div>
41+
<button *ngIf="hasNext(maxPage,page)" [disabled]="true" (click)="false" class="btn btn-default hasNext">...</button>
4042
<button [disabled]="(dataTable.offset + dataTable.limit) >= dataTable.itemCount" (click)="pageForward()" class="btn btn-default pagination-nextpage">&rsaquo;</button>
4143
<button [disabled]="(dataTable.offset + dataTable.limit) >= dataTable.itemCount" (click)="pageLast()" class="btn btn-default pagination-lastpage">&raquo;</button>
4244
</div>

0 commit comments

Comments
 (0)