@@ -10,6 +10,8 @@ import {
1010 ContentChildren ,
1111 Directive ,
1212 ElementRef ,
13+ IterableDiffers ,
14+ IterableDiffer ,
1315 OnDestroy ,
1416 QueryList ,
1517} from '@angular/core' ;
@@ -51,32 +53,33 @@ import {getTreeControlFunctionsMissingError} from './tree-errors';
5153 providers : [ { provide : CdkTreeNode , useExisting : CdkNestedTreeNode } ]
5254} )
5355export 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+
5459 /** The children data dataNodes of current node. They will be placed in `CdkTreeNodeOutlet`. */
5560 protected _children : T [ ] ;
5661
5762 /** The children node placeholder. */
5863 @ContentChildren ( CdkTreeNodeOutlet ) nodeOutlet : QueryList < CdkTreeNodeOutlet > ;
5964
6065 constructor ( protected _elementRef : ElementRef ,
61- protected _tree : CdkTree < T > ) {
66+ protected _tree : CdkTree < T > ,
67+ protected _differs : IterableDiffers ) {
6268 super ( _elementRef , _tree ) ;
6369 }
6470
6571 ngAfterContentInit ( ) {
72+ this . _dataDiffer = this . _differs . find ( [ ] ) . create ( ) ;
6673 if ( ! this . _tree . treeControl . getChildren ) {
6774 throw getTreeControlFunctionsMissingError ( ) ;
6875 }
6976 this . _tree . treeControl . getChildren ( this . data ) . pipe ( takeUntil ( this . _destroyed ) )
7077 . 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 ( ) ;
7780 } ) ;
7881 this . nodeOutlet . changes . pipe ( takeUntil ( this . _destroyed ) )
79- . subscribe ( ( _ ) => this . _addChildrenNodes ( ) ) ;
82+ . subscribe ( ( _ ) => this . updateChildrenNodes ( ) ) ;
8083 }
8184
8285 ngOnDestroy ( ) {
@@ -86,12 +89,10 @@ export class CdkNestedTreeNode<T> extends CdkTreeNode<T> implements AfterContent
8689 }
8790
8891 /** 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 ) ;
9596 }
9697 }
9798
0 commit comments