Skip to content

Commit 89e4bec

Browse files
Deilanazhukaudev
authored andcommitted
fix(table): fix a bug where source changes and title sort configuration doesn't get updated (#330)
1 parent 086cd1f commit 89e4bec

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

src/ng2-smart-table/components/thead/cells/title/title.component.ts

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Component, Input, Output, EventEmitter, OnInit } from '@angular/core';
1+
import { Component, Input, Output, EventEmitter, OnChanges, SimpleChanges } from '@angular/core';
2+
import { Subscription } from 'rxjs/Subscription';
23

34
import { DataSource } from '../../../../lib/data-source/data-source';
45
import { Column } from '../../../../lib/data-set/column';
@@ -16,27 +17,34 @@ import { Column } from '../../../../lib/data-set/column';
1617
<span class="ng2-smart-sort" *ngIf="!column.isSortable">{{ column.title }}</span>
1718
`,
1819
})
19-
export class TitleComponent implements OnInit {
20+
export class TitleComponent implements OnChanges {
2021

2122
currentDirection = '';
2223
@Input() column: Column;
2324
@Input() source: DataSource;
2425
@Output() sort = new EventEmitter<any>();
2526

26-
ngOnInit() {
27-
this.source.onChanged().subscribe((elements) => {
28-
const sortConf = this.source.getSort();
27+
protected dataChangedSub: Subscription;
2928

30-
if (sortConf.length > 0 && sortConf[0]['field'] === this.column.id) {
31-
this.currentDirection = sortConf[0]['direction'];
32-
} else {
33-
this.currentDirection = '';
29+
ngOnChanges(changes: SimpleChanges) {
30+
if (changes.source) {
31+
if (!changes.source.firstChange) {
32+
this.dataChangedSub.unsubscribe();
3433
}
34+
this.dataChangedSub = this.source.onChanged().subscribe((dataChanges) => {
35+
const sortConf = this.source.getSort();
3536

36-
sortConf.forEach((fieldConf: any) => {
37+
if (sortConf.length > 0 && sortConf[0]['field'] === this.column.id) {
38+
this.currentDirection = sortConf[0]['direction'];
39+
} else {
40+
this.currentDirection = '';
41+
}
3742

43+
sortConf.forEach((fieldConf: any) => {
44+
45+
});
3846
});
39-
});
47+
}
4048
}
4149

4250
_sort(event: any) {

0 commit comments

Comments
 (0)