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' ;
2
3
3
4
import { DataSource } from '../../../../lib/data-source/data-source' ;
4
5
import { Column } from '../../../../lib/data-set/column' ;
@@ -16,27 +17,34 @@ import { Column } from '../../../../lib/data-set/column';
16
17
<span class="ng2-smart-sort" *ngIf="!column.isSortable">{{ column.title }}</span>
17
18
` ,
18
19
} )
19
- export class TitleComponent implements OnInit {
20
+ export class TitleComponent implements OnChanges {
20
21
21
22
currentDirection = '' ;
22
23
@Input ( ) column : Column ;
23
24
@Input ( ) source : DataSource ;
24
25
@Output ( ) sort = new EventEmitter < any > ( ) ;
25
26
26
- ngOnInit ( ) {
27
- this . source . onChanged ( ) . subscribe ( ( elements ) => {
28
- const sortConf = this . source . getSort ( ) ;
27
+ protected dataChangedSub : Subscription ;
29
28
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 ( ) ;
34
33
}
34
+ this . dataChangedSub = this . source . onChanged ( ) . subscribe ( ( dataChanges ) => {
35
+ const sortConf = this . source . getSort ( ) ;
35
36
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
+ }
37
42
43
+ sortConf . forEach ( ( fieldConf : any ) => {
44
+
45
+ } ) ;
38
46
} ) ;
39
- } ) ;
47
+ }
40
48
}
41
49
42
50
_sort ( event : any ) {
0 commit comments