I have one paginator component to show the page list and detect when the user click to show another page and this component not change the page only must be trigger the event but I cannot watch the change event.
The component has an two-way binding to announce the current page changed and work fine because I can show the value in the html after change (please, see
in the HTML).
How I can execute the page change after change the current page in the paginator component?
I read the following Angular 2 change event - model changes but not work in my case.
HTML (using the paginator):
<paginator [pageCount]=pageCount [(currentPage)]=currentPage (ngModelChange)="pageChanged($event)"></paginator> <!--pageChanged is not called-->
<p>{{currentPage}}</p> <!--Work Fine showing the new page selected!-->
TS (paginator component):
import { Component, OnInit, OnChanges, SimpleChanges, Input, Output, EventEmitter, ChangeDetectorRef } from '@angular/core';
@Component({
selector: 'paginator',
templateUrl: './paginator.component.html',
styleUrls: ['./paginator.component.css']
})
export class PaginatorComponent implements OnInit, OnChanges {
pageList : number[] = [1];
@Input() pageCount : number = 1;
@Input() currentPage : number = 1;
@Output() currentPageChange = new EventEmitter<number>();
constructor(private ref:ChangeDetectorRef) {}
ngOnInit()
{
this.loadPageList();
}
ngOnChanges(changes: SimpleChanges)
{
this.loadPageList();
}
changePage(page : number)
{
if(page < 1)
return;
if(page > 1 && page > this.pageCount)
return;
if(this.currentPage == page)
return;
this.currentPage = page;
this.currentPageChange.emit(this.currentPage);
}
private loadPageList()
{
if(this.pageCount < 1)
this.pageCount = 1;
let pages = [1];
for(let p = 2; p <= this.pageCount; p++)
pages.push(p);
this.pageList = pages;
this.ref.detectChanges();
}
}
HTML (paginator component):
<div class="row">
<div class="col-md-12 text-center">
<ul class="pagination">
<li><a (click)="changePage(1)">«</a></li>
<li><a (click)="changePage(currentPage - 1)">❮</a></li>
<li *ngFor="let page of pageList" [class.active]="currentPage==page">
<a id="changePage{{page}}" (click)="changePage(page)">{{page}}</a>
</li>
<li><a (click)="changePage(currentPage + 1)">❯</a></li>
<li><a (click)="changePage(pageCount)">»</a></li>
</ul>
</div>
</div>