From 99319c5da8c7a77921c149919ddd46456cef2579 Mon Sep 17 00:00:00 2001 From: vthinkxie Date: Mon, 11 Mar 2019 17:03:21 +0800 Subject: [PATCH] fix(module:pagination): fix pagination bug close #3049 --- components/pagination/nz-pagination.component.ts | 16 ++++++++++------ components/pagination/nz-pagination.spec.ts | 9 +++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/components/pagination/nz-pagination.component.ts b/components/pagination/nz-pagination.component.ts index c3b1dff46f9..86043ff71e8 100644 --- a/components/pagination/nz-pagination.component.ts +++ b/components/pagination/nz-pagination.component.ts @@ -57,6 +57,12 @@ export class NzPaginationComponent implements OnInit, OnDestroy, OnChanges { } } + updatePageIndexValue(page: number): void { + this.nzPageIndex = page; + this.nzPageIndexChange.emit(this.nzPageIndex); + this.buildIndexes(); + } + isPageIndexValid(value: number): boolean { return this.validatePageIndex(value) === value; } @@ -65,8 +71,7 @@ export class NzPaginationComponent implements OnInit, OnDestroy, OnChanges { if (index !== this.nzPageIndex) { const pageIndex = this.validatePageIndex(index); if (pageIndex !== this.nzPageIndex) { - this.nzPageIndex = pageIndex; - this.nzPageIndexChange.emit(this.nzPageIndex); + this.updatePageIndexValue(pageIndex); } } } @@ -80,8 +85,7 @@ export class NzPaginationComponent implements OnInit, OnDestroy, OnChanges { this.nzPageSizeChange.emit($event); this.buildIndexes(); if (this.nzPageIndex > this.lastIndex) { - this.nzPageIndex = this.lastIndex; - this.nzPageIndexChange.emit(this.lastIndex); + this.updatePageIndexValue(this.lastIndex); } } @@ -89,8 +93,7 @@ export class NzPaginationComponent implements OnInit, OnDestroy, OnChanges { const target = input; const page = toNumber(target.value, this.nzPageIndex); if (isInteger(page) && this.isPageIndexValid(page) && page !== this.nzPageIndex) { - this.nzPageIndex = page; - this.nzPageIndexChange.emit(this.nzPageIndex); + this.updatePageIndexValue(page); } if (clearInputValue) { target.value = null; @@ -121,6 +124,7 @@ export class NzPaginationComponent implements OnInit, OnDestroy, OnChanges { } } this.pages = pages; + this.cdr.markForCheck(); } get lastIndex(): number { diff --git a/components/pagination/nz-pagination.spec.ts b/components/pagination/nz-pagination.spec.ts index 147d43025e5..263ba646a4c 100644 --- a/components/pagination/nz-pagination.spec.ts +++ b/components/pagination/nz-pagination.spec.ts @@ -66,6 +66,15 @@ describe('pagination', () => { fixture.detectChanges(); expect(testComponent.pageIndexChange).toHaveBeenCalledTimes(0); }); + it('should change pageIndex change pages list', () => { + fixture.detectChanges(); + testComponent.total = 500; + fixture.detectChanges(); + expect(paginationElement.children.length).toBe(9); + testComponent.pageIndex = 5; + fixture.detectChanges(); + expect(paginationElement.children.length).toBe(11); + }); it('should pre button disabled', () => { fixture.detectChanges(); expect(testComponent.pageIndexChange).toHaveBeenCalledTimes(0);