From a2202b4394ccf8bdf09caeb43e36206d6f36394a Mon Sep 17 00:00:00 2001 From: vthinkxie Date: Fri, 1 Mar 2019 18:00:00 +0800 Subject: [PATCH] fix(module:table): fix table crash with double binding (#3007) close #3004 --- components/table/nz-table.component.ts | 18 +++++---- components/table/nz-table.spec.ts | 53 +++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 9 deletions(-) diff --git a/components/table/nz-table.component.ts b/components/table/nz-table.component.ts index c1397a00c7..a6a5062777 100644 --- a/components/table/nz-table.component.ts +++ b/components/table/nz-table.component.ts @@ -122,7 +122,7 @@ export class NzTableComponent implements OnInit, AfterViewInit, OnDestroy, OnCha this.nzPageIndex = index; this.nzPageIndexChange.emit(this.nzPageIndex); } - this.updateFrontPaginationDataIfNeeded(); + this.updateFrontPaginationDataIfNeeded(this.nzPageSize !== size); } } @@ -177,15 +177,17 @@ export class NzTableComponent implements OnInit, AfterViewInit, OnDestroy, OnCha } } - updateFrontPaginationDataIfNeeded(): void { + updateFrontPaginationDataIfNeeded(isPageSizeOrDataChange: boolean = false): void { let data = []; if (this.nzFrontPagination) { this.nzTotal = this.nzData.length; - const maxPageIndex = Math.ceil(this.nzData.length / this.nzPageSize); - const pageIndex = !this.nzPageIndex ? 1 : (this.nzPageIndex > maxPageIndex ? maxPageIndex : this.nzPageIndex); - if (pageIndex !== this.nzPageIndex) { - this.nzPageIndex = pageIndex; - Promise.resolve().then(() => this.nzPageIndexChange.emit(pageIndex)); + if (isPageSizeOrDataChange) { + const maxPageIndex = Math.ceil(this.nzData.length / this.nzPageSize) || 1; + const pageIndex = this.nzPageIndex > maxPageIndex ? maxPageIndex : this.nzPageIndex; + if (pageIndex !== this.nzPageIndex) { + this.nzPageIndex = pageIndex; + Promise.resolve().then(() => this.nzPageIndexChange.emit(pageIndex)); + } } data = this.nzData.slice((this.nzPageIndex - 1) * this.nzPageSize, this.nzPageIndex * this.nzPageSize); } else { @@ -216,7 +218,7 @@ export class NzTableComponent implements OnInit, AfterViewInit, OnDestroy, OnCha this.setScrollPositionClassName(); } if (changes.nzPageIndex || changes.nzPageSize || changes.nzFrontPagination || changes.nzData) { - this.updateFrontPaginationDataIfNeeded(); + this.updateFrontPaginationDataIfNeeded(!!(changes.nzPageSize || changes.nzData)); } } diff --git a/components/table/nz-table.spec.ts b/components/table/nz-table.spec.ts index ce1c776c61..0f4aaa63a5 100644 --- a/components/table/nz-table.spec.ts +++ b/components/table/nz-table.spec.ts @@ -11,7 +11,7 @@ describe('nz-table', () => { beforeEach(async(() => { injector = TestBed.configureTestingModule({ imports : [ NzTableModule ], - declarations: [ NzTestTableBasicComponent, NzTestTableScrollComponent ] + declarations: [ NzTestTableBasicComponent, NzTestTableScrollComponent, NzTableSpecCrashComponent ] }); TestBed.compileComponents(); })); @@ -294,6 +294,19 @@ describe('nz-table', () => { expect(tableBody.scrollWidth).toBe(tableBody.clientWidth); }); }); + describe('double binding nz-table', () => { + let fixture; + let testComponent; + beforeEach(() => { + fixture = TestBed.createComponent(NzTableSpecCrashComponent); + fixture.detectChanges(); + testComponent = fixture.debugElement.componentInstance; + }); + it('should not crash when double binding pageSize and pageIndex', () => { + fixture.detectChanges(); + expect(testComponent.pageIndexChange).toHaveBeenCalledTimes(0); + }); + }); }); @Component({ @@ -441,3 +454,41 @@ export class NzTestTableScrollComponent implements OnInit { } } } + +/** https://github.com/NG-ZORRO/ng-zorro-antd/issues/3004 **/ +@Component({ + template: ` + + + + ID + NAME + + + + + + {{item.id}} + {{item.name}} + + + + + ` +}) +export class NzTableSpecCrashComponent { + data = []; + pageIndex = 1; + pageSize = 10; + pageIndexChange = jasmine.createSpy('pageSize callback'); + + constructor() { + setTimeout(() => { + this.data = new Array(100).fill(1).map((_, i) => ({ + id : i + 1, + name: `name ${i + 1}` + })); + }, 1000); + + } +}