Skip to content

Commit df18de2

Browse files
authored
Merge pull request #2 from faisal-iqbal/master
Seperate page number links in pagination
2 parents f21c85b + bbff28c commit df18de2

File tree

5 files changed

+80
-13
lines changed

5 files changed

+80
-13
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ The templates use bootstrap CSS class names, so the component requires a bootstr
1717

1818
Check out the [demo](https://ggmod.github.io/angular-2-data-table-demo) and its [code](https://github.com/MIt9/angular-4-data-table-demo) for examples of how to use it.
1919

20+
Demo for pagination with seperate page numbers will be added soon
21+
2022
## Installing:
2123
`npm install angular-4-data-table --save`
2224

@@ -35,6 +37,8 @@ add in .angular-cli.json
3537
and for prodaction build use
3638

3739
`ng build --prod --aot=false`
38-
40+
41+
### Added numerical pagination support with 2 next and previous links
42+
3943
#### Licensing
4044
MIT License

src/components/pagination.component.ts

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1-
import { Component, Inject, forwardRef } 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

15-
constructor(@Inject(forwardRef(() => DataTable)) public dataTable: DataTable) {}
15+
@Input() show_range = false;
16+
@Input() show_limit = false;
17+
@Input() show_input = false;
18+
@Input() show_numbers = true;
19+
20+
constructor(@Inject(forwardRef(() => DataTable)) public dataTable: DataTable) {
21+
}
1622

1723
pageBack() {
1824
this.dataTable.offset -= Math.min(this.dataTable.limit, this.dataTable.offset);
@@ -49,4 +55,44 @@ export class DataTablePagination {
4955
set page(value) {
5056
this.dataTable.page = Number(<any>value);
5157
}
58+
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[] {
81+
let items: number[] = [];
82+
if (number > 1) {
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++) {
93+
items.push(i);
94+
}
95+
}
96+
return items;
97+
}
5298
}

src/components/pagination.template.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export const PAGINATION_TEMPLATE = `
22
<div class="pagination-box">
3-
<div class="pagination-range">
3+
<div class="pagination-range" *ngIf="show_range">
44
{{dataTable.translations.paginationRange}}:
55
<span [textContent]="dataTable.offset + 1"></span>
66
-
@@ -9,7 +9,7 @@ export const PAGINATION_TEMPLATE = `
99
<span [textContent]="dataTable.itemCount"></span>
1010
</div>
1111
<div class="pagination-controllers">
12-
<div class="pagination-limit">
12+
<div class="pagination-limit" *ngIf="show_limit">
1313
<div class="input-group">
1414
<span class="input-group-addon">{{dataTable.translations.paginationLimit}}:</span>
1515
<input #limitInput type="number" class="form-control" min="1" step="1"
@@ -20,7 +20,7 @@ export const PAGINATION_TEMPLATE = `
2020
<div class=" pagination-pages">
2121
<button [disabled]="dataTable.offset <= 0" (click)="pageFirst()" class="btn btn-default pagination-firstpage">&laquo;</button>
2222
<button [disabled]="dataTable.offset <= 0" (click)="pageBack()" class="btn btn-default pagination-prevpage">&lsaquo;</button>
23-
<div class="pagination-page">
23+
<div class="pagination-page" *ngIf="show_input">
2424
<div class="input-group">
2525
<input #pageInput type="number" class="form-control" min="1" step="1" max="{{maxPage}}"
2626
[ngModel]="page" (blur)="page = pageInput.value"
@@ -31,6 +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>
35+
<div class="pagination-page" *ngIf="show_numbers">
36+
<button *ngFor="let i of createPageRange(maxPage,page)"
37+
[disabled]="i == page"
38+
(click)="page = i"
39+
class="btn btn-default">{{ i }}</button>
40+
</div>
41+
<button *ngIf="hasNext(maxPage,page)" [disabled]="true" (click)="false" class="btn btn-default hasNext">...</button>
3442
<button [disabled]="(dataTable.offset + dataTable.limit) >= dataTable.itemCount" (click)="pageForward()" class="btn btn-default pagination-nextpage">&rsaquo;</button>
3543
<button [disabled]="(dataTable.offset + dataTable.limit) >= dataTable.itemCount" (click)="pageLast()" class="btn btn-default pagination-lastpage">&raquo;</button>
3644
</div>

src/components/table.component.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ export class DataTable implements DataTableParams, OnInit {
4646
@Input() headerTitle: string;
4747
@Input() header = true;
4848
@Input() pagination = true;
49+
@Input() pagination_range = false;
50+
@Input() pagination_limit = false;
51+
@Input() pagination_input = false;
52+
@Input() pagination_numbers = true;
4953
@Input() indexColumn = true;
5054
@Input() indexColumnHeader = '';
5155
@Input() rowColors: RowCallback;

src/components/table.template.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ export const TABLE_TEMPLATE = `
4747
<div class="loading-cover" *ngIf="showReloading && reloading"></div>
4848
</div>
4949
50-
<data-table-pagination *ngIf="pagination"></data-table-pagination>
50+
<data-table-pagination
51+
*ngIf="pagination"
52+
[show_range]="pagination_range"
53+
[show_limit]="pagination_limit"
54+
[show_input]="pagination_input"
55+
[show_numbers]="pagination_numbers"></data-table-pagination>
5156
</div>
5257
`;

0 commit comments

Comments
 (0)