7
7
8
8
import { Directive , ElementRef , Input , OnDestroy , OnInit , Renderer2 , ViewContainerRef } from '@angular/core' ;
9
9
import { Subject } from 'rxjs' ;
10
- import { ADTSettings , ADTTemplateRefContext } from './models/settings' ;
10
+ import { ADTSettings , ADTColumns } from './models/settings' ;
11
11
12
12
@Directive ( {
13
13
selector : '[datatable]'
@@ -83,37 +83,8 @@ export class DataTableDirective implements OnDestroy, OnInit {
83
83
rowCallback : ( row , data , index ) => {
84
84
if ( resolvedDTOptions . columns ) {
85
85
const columns = resolvedDTOptions . columns ;
86
- // Filter columns with pipe declared
87
- const colsWithPipe = columns . filter ( x => x . ngPipeInstance && ! x . ngTemplateRef ) ;
88
- // Iterate
89
- colsWithPipe . forEach ( el => {
90
- const pipe = el . ngPipeInstance ;
91
- // find index of column using `data` attr
92
- const i = columns . findIndex ( e => e . data === el . data ) ;
93
- // get <td> element which holds data using index
94
- const rowFromCol = row . childNodes . item ( i ) ;
95
- // Transform data with Pipe
96
- const rowVal = $ ( rowFromCol ) . text ( ) ;
97
- const rowValAfter = pipe . transform ( rowVal ) ;
98
- // Apply transformed string to <td>
99
- $ ( rowFromCol ) . text ( rowValAfter ) ;
100
- } ) ;
101
-
102
- // Filter columns using `ngTemplateRef`
103
- const colsWithTemplate = columns . filter ( x => x . ngTemplateRef && ! x . ngPipeInstance ) ;
104
- colsWithTemplate . forEach ( el => {
105
- const { ref, context } = el . ngTemplateRef ;
106
- // get <td> element which holds data using index
107
- const i = columns . findIndex ( e => e . data === el . data ) ;
108
- const cellFromIndex = row . childNodes . item ( i ) ;
109
- // render onto DOM
110
- // finalize context to be sent to user
111
- const _context = Object . assign ( { } , context , context ?. userData , {
112
- adtData : data
113
- } ) ;
114
- const instance = self . vcr . createEmbeddedView ( ref , _context ) ;
115
- self . renderer . appendChild ( cellFromIndex , instance . rootNodes [ 0 ] ) ;
116
- } ) ;
86
+ this . applyNgPipeTransform ( row , columns ) ;
87
+ this . applyNgRefTemplate ( row , columns , data ) ;
117
88
}
118
89
119
90
// run user specified row callback if provided.
@@ -130,4 +101,39 @@ export class DataTableDirective implements OnDestroy, OnInit {
130
101
} ) ;
131
102
} ) ;
132
103
}
104
+
105
+ private applyNgPipeTransform ( row : Node , columns : ADTColumns [ ] ) : void {
106
+ // Filter columns with pipe declared
107
+ const colsWithPipe = columns . filter ( x => x . ngPipeInstance && ! x . ngTemplateRef ) ;
108
+ colsWithPipe . forEach ( el => {
109
+ const pipe = el . ngPipeInstance ;
110
+ // find index of column using `data` attr
111
+ const i = columns . findIndex ( e => e . data === el . data ) ;
112
+ // get <td> element which holds data using index
113
+ const rowFromCol = row . childNodes . item ( i ) ;
114
+ // Transform data with Pipe
115
+ const rowVal = $ ( rowFromCol ) . text ( ) ;
116
+ const rowValAfter = pipe . transform ( rowVal ) ;
117
+ // Apply transformed string to <td>
118
+ $ ( rowFromCol ) . text ( rowValAfter ) ;
119
+ } ) ;
120
+ }
121
+
122
+ private applyNgRefTemplate ( row : Node , columns : ADTColumns [ ] , data : Object ) : void {
123
+ // Filter columns using `ngTemplateRef`
124
+ const colsWithTemplate = columns . filter ( x => x . ngTemplateRef && ! x . ngPipeInstance ) ;
125
+ colsWithTemplate . forEach ( el => {
126
+ const { ref, context } = el . ngTemplateRef ;
127
+ // get <td> element which holds data using index
128
+ const i = columns . findIndex ( e => e . data === el . data ) ;
129
+ const cellFromIndex = row . childNodes . item ( i ) ;
130
+ // render onto DOM
131
+ // finalize context to be sent to user
132
+ const _context = Object . assign ( { } , context , context ?. userData , {
133
+ adtData : data
134
+ } ) ;
135
+ const instance = this . vcr . createEmbeddedView ( ref , _context ) ;
136
+ this . renderer . appendChild ( cellFromIndex , instance . rootNodes [ 0 ] ) ;
137
+ } ) ;
138
+ }
133
139
}
0 commit comments