Skip to content

Commit 20b0d2a

Browse files
authored
Add support for multiple datatable custom html templates (#17)
1 parent d671487 commit 20b0d2a

File tree

6 files changed

+42
-13
lines changed

6 files changed

+42
-13
lines changed

projects/angular-components/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@frankframework/angular-components",
3-
"version": "1.0.2",
3+
"version": "1.1.0",
44
"description": "A collection of reusable components designed for use in Frank!Framework projects",
55
"main": "",
66
"author": "Vivy Booman",

projects/angular-components/src/lib/datatable/datatable.component.html

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,28 @@
2929
><ng-container>{{ element[column.property] }}</ng-container>
3030
</ng-container>
3131
<ng-template #htmlBody>
32-
<ng-container
33-
*ngIf="content; else noTemplate"
34-
[ngTemplateOutlet]="content.templateReference"
35-
[ngTemplateOutletContext]="{ rowElement: element }"
36-
></ng-container>
37-
<ng-template #noTemplate><small>No template with DtContent found in datatable body</small></ng-template>
32+
<ng-container *ngIf="contentTemplates.length > 0; else noTemplate">
33+
<ng-container
34+
*ngIf="contentTemplates.length === 1; else multiTemplates"
35+
[ngTemplateOutlet]="contentTemplates[0].template"
36+
[ngTemplateOutletContext]="{ rowElement: element }"
37+
></ng-container>
38+
<ng-template #multiTemplates>
39+
<ng-container
40+
*ngIf="findHtmlTemplate(column.name); else noTemplateWithName"
41+
[ngTemplateOutlet]="findHtmlTemplate(column.name)!.template"
42+
[ngTemplateOutletContext]="{ rowElement: element }"
43+
></ng-container>
44+
</ng-template>
45+
</ng-container>
3846
</ng-template>
3947
</td>
4048
</ng-container>
4149
</ng-container>
50+
<ng-template #noTemplate><small>No template with DtContent found in datatable body</small></ng-template>
51+
<ng-template #noTemplateWithName
52+
><small>No template with DtContent name found in datatable body</small></ng-template
53+
>
4254

4355
<tr cdk-header-row *cdkHeaderRowDef="displayedColumns"></tr>
4456
<tr cdk-row *cdkRowDef="let row; columns: displayedColumns"></tr>

projects/angular-components/src/lib/datatable/datatable.component.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { AfterViewInit, Component, ContentChild, Input, OnDestroy } from '@angular/core';
1+
import { AfterViewInit, Component, ContentChildren, Input, OnDestroy, QueryList, TemplateRef } from '@angular/core';
22
import { CdkTableModule, DataSource } from '@angular/cdk/table';
33
import { CommonModule } from '@angular/common';
44
import { FormsModule } from '@angular/forms';
55
import { BehaviorSubject, Observable, Subscription } from 'rxjs';
6-
import { DtContentDirective } from './dt-content.directive';
6+
import { DtContentDirective, DtContent } from './dt-content.directive';
77

88
export type TableOptions = {
99
sizeOptions: number[];
@@ -48,6 +48,11 @@ export type DataTableServerResponseInfo<T> = {
4848
data: T[];
4949
};
5050

51+
type ContentTemplate<T> = {
52+
template: TemplateRef<DtContent<T>>;
53+
name?: string;
54+
};
55+
5156
@Component({
5257
selector: 'ff-datatable',
5358
standalone: true,
@@ -59,7 +64,8 @@ export class DatatableComponent<T> implements AfterViewInit, OnDestroy {
5964
@Input({ required: true }) public datasource!: DataTableDataSource<T>;
6065
@Input({ required: true }) public displayColumns: DataTableColumn<T>[] = [];
6166

62-
@ContentChild(DtContentDirective) protected content?: DtContentDirective<T>;
67+
@ContentChildren(DtContentDirective) protected content!: QueryList<DtContentDirective<T>>;
68+
protected contentTemplates: ContentTemplate<T>[] = [];
6369
protected totalFilteredEntries: number = 0;
6470
protected totalEntries: number = 0;
6571
protected minPageEntry: number = 0;
@@ -76,6 +82,10 @@ export class DatatableComponent<T> implements AfterViewInit, OnDestroy {
7682
ngAfterViewInit(): void {
7783
// needed to avoid ExpressionChangedAfterItHasBeenCheckedError
7884
setTimeout(() => {
85+
this.contentTemplates = this.content.map((contentItem) => ({
86+
name: contentItem.dtContent,
87+
template: contentItem.templateReference,
88+
}));
7989
const entriesSubscription = this.datasource.getEntriesInfo().subscribe((entriesInfo) => {
8090
this.totalEntries = entriesInfo.totalEntries;
8191
this.totalFilteredEntries = entriesInfo.totalFilteredEntries;
@@ -107,6 +117,10 @@ export class DatatableComponent<T> implements AfterViewInit, OnDestroy {
107117
updatePage(pageNumber: number): void {
108118
this.datasource.updatePage(pageNumber);
109119
}
120+
121+
protected findHtmlTemplate(templateName: string): ContentTemplate<T> | undefined {
122+
return this.contentTemplates.find(({ name }) => name === templateName);
123+
}
110124
}
111125

112126
export class DataTableDataSource<T> extends DataSource<T> {
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import { Directive, TemplateRef } from '@angular/core';
1+
import { Directive, Input, TemplateRef } from '@angular/core';
22

33
export interface DtContent<T> {
44
rowElement: T;
55
}
66

77
@Directive({
8-
selector: '[appDtContent]',
8+
selector: '[dtContent]',
99
standalone: true,
1010
})
1111
export class DtContentDirective<T> {
1212
constructor(public templateReference: TemplateRef<DtContent<T>>) {}
13+
@Input() dtContent?: string;
1314
}

src/app/app.component.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,10 @@ <h2>Chip</h2>
8080
<h2>Datatable</h2>
8181
<div class="components">
8282
<ff-datatable [datasource]="datasource" [displayColumns]="displayedColumns">
83-
<ng-template appDtContent>
83+
<ng-template dtContent="actions">
8484
<span><button>Something</button></span>
8585
</ng-template>
86+
<ng-template dtContent="something"> Something </ng-template>
8687
</ff-datatable>
8788
</div>
8889
<p>Without template:</p>

src/app/app.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export class AppComponent implements OnInit {
2626
{ name: 'description', displayName: 'Description', property: 'description' },
2727
{ name: 'genre', displayName: 'Genre', property: 'genre' },
2828
{ name: 'actions', displayName: 'Actions', property: null, html: true },
29+
{ name: 'something', displayName: 'Something', property: null, html: true },
2930
];
3031

3132
constructor() {}

0 commit comments

Comments
 (0)