Let me simplify my issue:
- I am currently using
NgxDatatable
to display a CRUD table. - I have a base component named
CrudComponent
, which manages all CRUD operations. - This component was designed to be extended for all basic entities.
The challenge I am encountering now is finding a way for child components to somehow inject custom cellTemplate
.
I want to avoid duplicating code, so I don't have to repeatedly copy the parent template just to add 1-2 ng-template
.
For instance, in CrudComponent
:
@ViewChild('actionsCellRenderer') actionsCellRenderer: TemplateRef<any>;
And in the template:
<ng-template #actionsCellRenderer let-row="row" let-value="value">
<button mat-icon-button (click)="startEdit(row)">
<fa-icon [icon]="['far', 'edit']"></fa-icon>
</button>
<button mat-icon-button (click)="startDelete(row)" color="warn">
<fa-icon [icon]="['far', 'trash-alt']"></fa-icon>
</button>
</ng-template>
If I extend this CrudComponent
with MovieComponent
, I'd have to manually copy the entire HTML template into the new MovieComponent.html
file and add:
@ViewChild('ratingCellRenderer') ratingCellRenderer: TemplateRef<any>;
<ng-template #ratingCellRenderer let-row="row" let-value="value">
<bar-rating
[(rate)]="value"
[max]="10"
theme="horizontal"
readOnly="true"
></bar-rating>
</ng-template>
Potential solutions:
- One approach could be utilizing a template pre-compiler like
twig
. Is this feasible? If so, how? - Or a more Angular-centric solution. Essentially creating a table of
TemplateRef<any>
instances like this:
cellRenderers = {
rating: new TemplateRef<any>(), //but how do I manually create TemplateRef?
picture: new TemplateRef<any>(),
}
Then in the NgxDatatable
columns definition:
{name: 'Score', prop: 'score', cellTemplate: this.cellRenderers['rating']},
- OR perhaps there's another, more elegant way to address this concern?