Skip to content
This repository was archived by the owner on Feb 2, 2025. It is now read-only.

Commit 1a04d9c

Browse files
committed
feat(core): Add template-ref support
1 parent aebd56f commit 1a04d9c

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

src/angular-datatables.directive.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
* found in the LICENSE file at https://raw.githubusercontent.com/l-lin/angular-datatables/master/LICENSE
66
*/
77

8-
import { Directive, ElementRef, Input, OnDestroy, OnInit } from '@angular/core';
8+
import { Directive, ElementRef, Input, OnDestroy, OnInit, Renderer2, ViewContainerRef } from '@angular/core';
99
import { Subject } from 'rxjs';
10-
import { ADTSettings } from './models/settings';
10+
import { ADTSettings, ADTTemplateRefContext } from './models/settings';
1111

1212
@Directive({
1313
selector: '[datatable]'
@@ -37,7 +37,11 @@ export class DataTableDirective implements OnDestroy, OnInit {
3737
// Only used for destroying the table when destroying this directive
3838
private dt: DataTables.Api;
3939

40-
constructor(private el: ElementRef) { }
40+
constructor(
41+
private el: ElementRef,
42+
private vcr: ViewContainerRef,
43+
private renderer: Renderer2
44+
) { }
4145

4246
ngOnInit(): void {
4347
if (this.dtTrigger) {
@@ -59,6 +63,7 @@ export class DataTableDirective implements OnDestroy, OnInit {
5963
}
6064

6165
private displayTable(): void {
66+
const self = this;
6267
this.dtInstance = new Promise((resolve, reject) => {
6368
Promise.resolve(this.dtOptions).then(dtOptions => {
6469
// Using setTimeout as a "hack" to be "part" of NgZone
@@ -69,7 +74,7 @@ export class DataTableDirective implements OnDestroy, OnInit {
6974
if (dtOptions.columns) {
7075
const columns = dtOptions.columns;
7176
// Filter columns with pipe declared
72-
const colsWithPipe = columns.filter(x => x.ngPipeInstance);
77+
const colsWithPipe = columns.filter(x => x.ngPipeInstance && !x.ngTemplateRef);
7378
// Iterate
7479
colsWithPipe.forEach(el => {
7580
const pipe = el.ngPipeInstance;
@@ -83,6 +88,22 @@ export class DataTableDirective implements OnDestroy, OnInit {
8388
// Apply transformed string to <td>
8489
$(rowFromCol).text(rowValAfter);
8590
});
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+
});
86107
}
87108

88109
// run user specified row callback if provided.

src/models/settings.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
1-
import { PipeTransform } from '@angular/core';
1+
import { PipeTransform, TemplateRef } from '@angular/core';
22

33
export interface ADTSettings extends DataTables.Settings {
44
columns?: ADTColumns[];
55
}
66
export interface ADTColumns extends DataTables.ColumnSettings {
77
/** Set instance of Angular pipe to transform the data of particular column */
88
ngPipeInstance?: PipeTransform;
9+
/** Set `TemplateRef` to transform the data of this column */
10+
ngTemplateRef?: ADTTemplateRef;
11+
}
12+
13+
export interface ADTTemplateRef {
14+
/** `TemplateRef` to work with */
15+
ref: TemplateRef<any>;
16+
/** */
17+
context?: ADTTemplateRefContext;
18+
}
19+
20+
export interface ADTTemplateRefContext {
21+
captureEvents: Function;
22+
userData?: any;
923
}

0 commit comments

Comments
 (0)