diff --git a/components/table/src/sort/sort.spec.ts b/components/table/src/sort/sort.spec.ts index 033ec05174..128b6bbbe9 100644 --- a/components/table/src/sort/sort.spec.ts +++ b/components/table/src/sort/sort.spec.ts @@ -166,6 +166,17 @@ describe('DtSort', () => { expect(checkCellsSorted(component.cells, true, 'column_a')).toBeTruthy(); }); + it('should sort the correct column when active is being changed dynamically', () => { + component.dataSource = DATA_SOURCE; + component.start = 'asc'; + component.active = 'column_a'; + fixture.detectChanges(); + + component.active = 'column_b'; + fixture.detectChanges(); + expect(checkCellsSorted(component.cells, true, 'column_b')).toBeTruthy(); + }); + it('should keep all cells sorted if there are new rows added dynamically', () => { component.dataSource = DATA_SOURCE; component.start = 'asc'; diff --git a/components/table/src/sort/sort.ts b/components/table/src/sort/sort.ts index ca06ce30ed..3d7b5b04ee 100644 --- a/components/table/src/sort/sort.ts +++ b/components/table/src/sort/sort.ts @@ -156,6 +156,12 @@ export class DtSort extends _DtSortMixinBase ) { this.direction = this.start; } + + // If active is bound and being changed after initialization + // we need to update the sorter. + if (isDefined(changes.active) && !changes.active.firstChange) { + this.sort(this.active, this.direction); + } this._stateChanges.next(); }