Last active
January 8, 2019 12:34
-
-
Save Mulperi/6a5c5aa8563eb24e080f337226bf1a83 to your computer and use it in GitHub Desktop.
Probably the world's simplest paginator with Angular.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component } from '@angular/core'; | |
@Component({ | |
selector: 'app-paginator', | |
template: ` | |
<div *ngFor="let item of items | slice:(page*itemsPerPage):(page*itemsPerPage+itemsPerPage)"> | |
{{ item.title }} | |
</div> | |
<button [disabled]="page == 0" (click)="onPreviousClick()">previous page</button> | |
<button [disabled]="isLastPage()" (click)="onNextClick()">next page</button> | |
<p>page {{ page + 1 }} of {{ getPages() }}</p> | |
`, | |
styleUrls: ['./paginator.component.css'] | |
}) | |
export class PaginatorComponent { | |
items = [ | |
{ | |
id: 1, | |
title: "item 1 title" | |
}, | |
{ | |
id: 2, | |
title: "item 2 title" | |
}, | |
{ | |
id: 3, | |
title: "item 3 title" | |
}, | |
{ | |
id: 4, | |
title: "item 4 title" | |
}, | |
{ | |
id: 5, | |
title: "item 5 title" | |
}, | |
{ | |
id: 6, | |
title: "item 6 title" | |
}, | |
{ | |
id: 7, | |
title: "item 7 title" | |
}, | |
{ | |
id: 8, | |
title: "item 8 title" | |
}, | |
{ | |
id: 9, | |
title: "item 9 title" | |
}, | |
{ | |
id: 10, | |
title: "item 10 title" | |
}, | |
] | |
itemsPerPage = 4; | |
page = 0; | |
onPreviousClick() { | |
this.page--; | |
} | |
onNextClick() { | |
this.page++; | |
} | |
isLastPage() { | |
return this.page >= this.items.length / this.itemsPerPage - 1; | |
} | |
getPages() { | |
return Math.ceil(this.items.length / this.itemsPerPage); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment