5
5
* found in the LICENSE file at https://raw.githubusercontent.com/l-lin/angular-datatables/master/LICENSE
6
6
*/
7
7
8
- import { Directive , ElementRef , Input , OnDestroy , OnInit } from '@angular/core' ;
8
+ import { Directive , ElementRef , Input , OnDestroy , OnInit , Renderer2 , ViewContainerRef } from '@angular/core' ;
9
9
import { Subject } from 'rxjs' ;
10
- import { ADTSettings } from './models/settings' ;
10
+ import { ADTSettings , ADTTemplateRefContext } from './models/settings' ;
11
11
12
12
@Directive ( {
13
13
selector : '[datatable]'
@@ -37,7 +37,11 @@ export class DataTableDirective implements OnDestroy, OnInit {
37
37
// Only used for destroying the table when destroying this directive
38
38
private dt : DataTables . Api ;
39
39
40
- constructor ( private el : ElementRef ) { }
40
+ constructor (
41
+ private el : ElementRef ,
42
+ private vcr : ViewContainerRef ,
43
+ private renderer : Renderer2
44
+ ) { }
41
45
42
46
ngOnInit ( ) : void {
43
47
if ( this . dtTrigger ) {
@@ -59,6 +63,7 @@ export class DataTableDirective implements OnDestroy, OnInit {
59
63
}
60
64
61
65
private displayTable ( ) : void {
66
+ const self = this ;
62
67
this . dtInstance = new Promise ( ( resolve , reject ) => {
63
68
Promise . resolve ( this . dtOptions ) . then ( dtOptions => {
64
69
// Using setTimeout as a "hack" to be "part" of NgZone
@@ -69,7 +74,7 @@ export class DataTableDirective implements OnDestroy, OnInit {
69
74
if ( dtOptions . columns ) {
70
75
const columns = dtOptions . columns ;
71
76
// Filter columns with pipe declared
72
- const colsWithPipe = columns . filter ( x => x . ngPipeInstance ) ;
77
+ const colsWithPipe = columns . filter ( x => x . ngPipeInstance && ! x . ngTemplateRef ) ;
73
78
// Iterate
74
79
colsWithPipe . forEach ( el => {
75
80
const pipe = el . ngPipeInstance ;
@@ -83,6 +88,22 @@ export class DataTableDirective implements OnDestroy, OnInit {
83
88
// Apply transformed string to <td>
84
89
$ ( rowFromCol ) . text ( rowValAfter ) ;
85
90
} ) ;
91
+
92
+ // Filter columns using `ngTemplateRef`
93
+ const colsWithTemplate = columns . filter ( x => x . ngTemplateRef && ! x . ngPipeInstance ) ;
94
+ colsWithTemplate . forEach ( el => {
95
+ const { ref, context } = el . ngTemplateRef ;
96
+ // get <td> element which holds data using index
97
+ const index = columns . findIndex ( e => e . data == el . data ) ;
98
+ const cellFromIndex = row . childNodes . item ( index ) ;
99
+ // render onto DOM
100
+ // finalize context to be sent to user
101
+ const _context = Object . assign ( { } , context , context ?. userData , {
102
+ adtData : data
103
+ } ) ;
104
+ const instance = self . vcr . createEmbeddedView ( ref , _context ) ;
105
+ self . renderer . appendChild ( cellFromIndex , instance . rootNodes [ 0 ] ) ;
106
+ } ) ;
86
107
}
87
108
88
109
// run user specified row callback if provided.
0 commit comments