Skip to content

Commit da5cae4

Browse files
author
Faisal Iqbal
committed
Adding page numbers and making range, limit and input optional
1 parent f21c85b commit da5cae4

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

src/components/pagination.component.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, Inject, forwardRef } from '@angular/core';
1+
import { Component, Inject, forwardRef, Input } from '@angular/core';
22
import { DataTable } from './table.component';
33
import { PAGINATION_TEMPLATE } from './pagination.template';
44
import { PAGINATION_STYLE } from "./pagination.style";
@@ -12,6 +12,11 @@ import { PAGINATION_STYLE } from "./pagination.style";
1212
})
1313
export class DataTablePagination {
1414

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

1722
pageBack() {
@@ -49,4 +54,14 @@ export class DataTablePagination {
4954
set page(value) {
5055
this.dataTable.page = Number(<any>value);
5156
}
57+
58+
createPageRange(number): any[] {
59+
let items: number[] = [];
60+
if (number > 1) {
61+
for (let i = 1; i <= number; i++) {
62+
items.push(i);
63+
}
64+
}
65+
return items;
66+
}
5267
}

src/components/pagination.template.ts

Lines changed: 8 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,11 @@ export const PAGINATION_TEMPLATE = `
3131
</div>
3232
</div>
3333
</div>
34+
<button *ngIf="show_numbers"
35+
*ngFor="let i of createPageRange(dataTable.lastPage)"
36+
[disabled]="i == page"
37+
(click)="page = i"
38+
class="btn btn-default">{{ i }}</button>
3439
<button [disabled]="(dataTable.offset + dataTable.limit) >= dataTable.itemCount" (click)="pageForward()" class="btn btn-default pagination-nextpage">&rsaquo;</button>
3540
<button [disabled]="(dataTable.offset + dataTable.limit) >= dataTable.itemCount" (click)="pageLast()" class="btn btn-default pagination-lastpage">&raquo;</button>
3641
</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)