@@ -10,6 +10,8 @@ import {
10
10
ContentChildren ,
11
11
Directive ,
12
12
ElementRef ,
13
+ IterableDiffers ,
14
+ IterableDiffer ,
13
15
OnDestroy ,
14
16
QueryList ,
15
17
} from '@angular/core' ;
@@ -51,32 +53,33 @@ import {getTreeControlFunctionsMissingError} from './tree-errors';
51
53
providers : [ { provide : CdkTreeNode , useExisting : CdkNestedTreeNode } ]
52
54
} )
53
55
export class CdkNestedTreeNode < T > extends CdkTreeNode < T > implements AfterContentInit , OnDestroy {
56
+ /** Differ used to find the changes in the data provided by the data source. */
57
+ private _dataDiffer : IterableDiffer < T > ;
58
+
54
59
/** The children data dataNodes of current node. They will be placed in `CdkTreeNodeOutlet`. */
55
60
protected _children : T [ ] ;
56
61
57
62
/** The children node placeholder. */
58
63
@ContentChildren ( CdkTreeNodeOutlet ) nodeOutlet : QueryList < CdkTreeNodeOutlet > ;
59
64
60
65
constructor ( protected _elementRef : ElementRef ,
61
- protected _tree : CdkTree < T > ) {
66
+ protected _tree : CdkTree < T > ,
67
+ protected _differs : IterableDiffers ) {
62
68
super ( _elementRef , _tree ) ;
63
69
}
64
70
65
71
ngAfterContentInit ( ) {
72
+ this . _dataDiffer = this . _differs . find ( [ ] ) . create ( ) ;
66
73
if ( ! this . _tree . treeControl . getChildren ) {
67
74
throw getTreeControlFunctionsMissingError ( ) ;
68
75
}
69
76
this . _tree . treeControl . getChildren ( this . data ) . pipe ( takeUntil ( this . _destroyed ) )
70
77
. subscribe ( result => {
71
- if ( result && result . length ) {
72
- // In case when nodeOutlet is not in the DOM when children changes, save it in the node
73
- // and add to nodeOutlet when it's available.
74
- this . _children = result as T [ ] ;
75
- this . _addChildrenNodes ( ) ;
76
- }
78
+ this . _children = result ;
79
+ this . updateChildrenNodes ( ) ;
77
80
} ) ;
78
81
this . nodeOutlet . changes . pipe ( takeUntil ( this . _destroyed ) )
79
- . subscribe ( ( _ ) => this . _addChildrenNodes ( ) ) ;
82
+ . subscribe ( ( _ ) => this . updateChildrenNodes ( ) ) ;
80
83
}
81
84
82
85
ngOnDestroy ( ) {
@@ -86,12 +89,10 @@ export class CdkNestedTreeNode<T> extends CdkTreeNode<T> implements AfterContent
86
89
}
87
90
88
91
/** Add children dataNodes to the NodeOutlet */
89
- protected _addChildrenNodes ( ) : void {
90
- this . _clear ( ) ;
91
- if ( this . nodeOutlet . length && this . _children && this . _children . length ) {
92
- this . _children . forEach ( ( child , index ) => {
93
- this . _tree . insertNode ( child , index , this . nodeOutlet . first . viewContainer ) ;
94
- } ) ;
92
+ protected updateChildrenNodes ( ) : void {
93
+ if ( this . nodeOutlet . length && this . _children ) {
94
+ const viewContainer = this . nodeOutlet . first . viewContainer ;
95
+ this . _tree . renderNodeChanges ( this . _children , this . _dataDiffer , viewContainer ) ;
95
96
}
96
97
}
97
98
0 commit comments