Skip to content

Commit

Permalink
fix(module:pagination): fix pagination bug (#3067)
Browse files Browse the repository at this point in the history
close #3049
  • Loading branch information
vthinkxie authored Mar 11, 2019
1 parent dbc33ae commit f4948d7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
16 changes: 10 additions & 6 deletions components/pagination/nz-pagination.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
}
}
}
Expand All @@ -80,17 +85,15 @@ 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);
}
}

handleKeyDown(_: KeyboardEvent, input: HTMLInputElement, clearInputValue: boolean): void {
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;
Expand Down Expand Up @@ -121,6 +124,7 @@ export class NzPaginationComponent implements OnInit, OnDestroy, OnChanges {
}
}
this.pages = pages;
this.cdr.markForCheck();
}

get lastIndex(): number {
Expand Down
9 changes: 9 additions & 0 deletions components/pagination/nz-pagination.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f4948d7

Please sign in to comment.