File
Metadata
| encapsulation |
ViewEncapsulation.None |
| host |
{ } |
| selector |
stbui-pagination |
| styleUrls |
./pagination.component.scss |
| templateUrl |
./pagination.component.html |
Index
Properties
|
|
|
Methods
|
|
|
Inputs
|
|
|
Outputs
|
|
|
Accessors
|
|
|
|
currentPage
|
Type : number
|
Default value : 1
|
|
|
Outputs
|
onPage
|
Type : EventEmitter<any>
|
|
|
Methods
|
onPageTriggered
|
onPageTriggered(page: number)
|
|
|
Parameters :
| Name |
Type |
Optional |
| page |
number
|
No
|
|
|
Private
_align
|
Type : "start" | "center" | "end"
|
Default value : 'start'
|
|
|
|
Private
_total
|
Type : number
|
Default value : 10
|
|
|
|
pages
|
Type : number[]
|
Default value : []
|
|
|
Accessors
|
total
|
gettotal()
|
|
|
settotal(value)
|
|
|
|
|
import {
Component,
Input,
Output,
EventEmitter,
ViewEncapsulation
} from '@angular/core';
@Component({
selector: 'stbui-pagination',
templateUrl: './pagination.component.html',
styleUrls: ['./pagination.component.scss'],
host: {
'[class.pagination-end]': '_isEnd',
'[class.pagination-center]': '_isCenter'
},
encapsulation: ViewEncapsulation.None
})
export class PaginationComponent {
@Input()
set total(value) {
this.totalPages = value;
this.setPage();
this._total = value;
}
get total() {
return this._total;
}
@Output() onPage: EventEmitter<any> = new EventEmitter<any>();
private _align: 'start' | 'center' | 'end' = 'start';
@Input()
get align() {
return this._align;
}
set align(value) {
value = value == 'end' ? 'end' : value == 'center' ? 'center' : 'start';
if (value != this._align) {
this._align = value;
}
}
get _isEnd() {
return this._align == 'end';
}
get _isCenter() {
return this._align == 'center';
}
private _total: number = 10;
@Input() currentPage: number = 1;
totalPages: number;
pages: number[] = [];
constructor() {}
setPage() {
let start: number = 0;
let end: number = 0;
if (this.currentPage - 5 <= 0) {
start = 0;
end = start + 10;
} else if (this.currentPage + 5 > this.totalPages) {
end = this.totalPages;
start = end - 10;
} else {
start = this.currentPage - 6;
end = this.currentPage + 5;
}
let pages = [];
for (var i = 1; i <= this.totalPages; i++) {
pages.push(i);
}
this.pages = pages.slice(start, end);
}
onPageTriggered(page: number) {
if (this.currentPage == page) {
return false;
}
this.currentPage = page;
if (page > 5 || page === 1) {
this.setPage();
}
this.onPage.emit(page);
}
}
<ul class="pagination">
<li *ngIf="currentPage > 6" (click)="onPageTriggered(1)">
<mat-icon>chevron_left</mat-icon>
</li>
<li mat-ripple [class.active]="currentPage == page" (click)="onPageTriggered(page)" *ngFor="let page of pages">
{{ page }}
</li>
<li *ngIf="currentPage < totalPages - 4" (click)="onPageTriggered(totalPages)">
<mat-icon>chevron_right</mat-icon>
</li>
</ul>
$pagination-font-size-base: 14px !default;
$pagination-size: 32px !default;
$pagination-line-height: 32px !default;
$pagination-padding: 0 8px !default;
$pagination-margin: 0 8px !default;
$pagination-border-radius: 2px !default;
:host {
display: flex;
justify-content: center;
}
.pagination {
display: flex;
justify-content: flex-start;
align-items: center;
flex-wrap: wrap;
user-select: none;
cursor: pointer;
margin: 0;
padding: 0;
li {
box-sizing: border-box;
display: flex;
justify-content: center;
align-items: center;
font-size: $pagination-font-size-base;
font-weight: 500;
text-align: center;
height: $pagination-size;
min-width: $pagination-size;
padding: $pagination-padding;
margin: $pagination-margin;
line-height: $pagination-line-height;
position: relative;
border-radius: $pagination-border-radius;
list-style: none;
}
&-end {
display: flex;
justify-content: flex-end;
}
&-center {
display: flex;
justify-content: center;
}
}
Legend
Html element with directive