Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular ngx-datatable cellTemplate not loading

I'm trying to override the template of an ngx-datatable cell. So in my template (html) file i'm setting a small view for this. To test if the template works i'm just displaying the value with starts around it:

<ngx-datatable
    class="material"
    [rows]="rows"
    [columns]="columns"
    headerHeight="45">
</ngx-datatable>
<ng-template #roleTemplate let-row="row" let-value="value" let-i="index">
  <strong> **{{ value }}** </strong>
</ng-template>

In my component im using ViewChild to get the template and give it to my datatable. @ViewChild('roleTemplate') roleTemplate: TemplateRef<any>;

public columns = [
        { name: 'Name', prop: 'displayName' },
        { name: 'Email', prop: 'emailAddress' },
        { name: 'Role', prop: 'role', cellTemplate: this.roleTemplate },
        { name: 'Status', prop: 'status' },
    ];

Only thing the documentation says:

cellTemplate: TemplateRef

Angular TemplateRef allowing you to author custom body cell templates

However it doesn't seem to work. Am I missing something?

like image 939
Mike Bovenlander Avatar asked May 11 '17 06:05

Mike Bovenlander


1 Answers

You have to initialize your columns inside ngAfterViewInit. I was facing the same problem and I realized the templateRef was undefined, even if I initialized the columns inside ngOnInit. So I moved the columns initialization to ngAfterViewInit and it works just great. Like following in Angular 9:

import { ... AfterViewInit } from '@angular/core';

ngAfterViewInit() {
    this.columns = [
        { name: 'Name', prop: 'displayName' },
        { name: 'Email', prop: 'emailAddress' },
        { name: 'Role', prop: 'role', cellTemplate: this.roleTemplate },
        { name: 'Status', prop: 'status' },
    ];
  }
like image 107
gbsojo Avatar answered Sep 28 '22 04:09

gbsojo