2

Back in AngularJS, I created a simple smart table component that could be used like this for our team:

<smart-table items="vm.people">
    <column field="name">{{row.firstname}} {{row.lastname}}</column>
    <column field="age">{{vm.getAge(row.birthday)}}</column>
</smart-table>

This is a contrived example, but it worked like this. it generated the table, with the headers and used the inner content of the <column> tag as the template for each cell (the <td> element) correctly.

Now, I am trying to port this to Angular (6+). So far, using @ContentChildren I am able to easily extract the list of columns.

import { Component, OnInit, Input, ContentChildren, QueryList } from '@angular/core';

@Component({
    selector: 'app-root',
    template: `
<app-table [data]="data">
    <app-column title="name">{{name}}</app-column>
    <app-column title="age">{{birthday}}</app-column>
</app-table>
`,
})
export class AppComponent {
    data = [{
        name: 'Lorem Ipsum',
        birthday: new Date(1980, 1, 23),
    }, {
        name: 'John Smith',
        birthday: new Date(1990, 4, 5),
    }, {
        name: 'Jane Doe',
        birthday: new Date(2000, 6, 7),

    }];
}

@Component({
    selector: 'app-column',
    template: ``,
})
export class ColumnComponent implements OnInit {
    @Input() title: string;

    constructor() { }

    ngOnInit() {
    }

}

@Component({
    selector: 'app-table',
    template: `
<table>
    <thead>
    <th *ngFor="let column of columns">{{column.title}}</th>
    </thead>
    <tbody>
    <tr *ngFor="let row of data">
        <td *ngFor="let column of columns">
        <!-- <ng-container *ngTemplateOutlet="column.title;context:row" ></ng-container> -->
        </td>
    </tr>
    </tbody>
</table>
`,
})
export class TableComponent implements OnInit {
    @Input() data: any[];

    @ContentChildren(ColumnComponent) columns: QueryList<ColumnComponent>;

    constructor() { }

    ngOnInit() {
    }

}

This renders the following HTML:

<table>
    <thead>
        <!--bindings={"ng-reflect-ng-for-of": "[object Object],[object Object"}-->
        <th>name</th>
        <th>age</th>
    </thead>
    <tbody>
        <!--bindings={"ng-reflect-ng-for-of": "[object Object],[object Object"}-->
        <tr>
            <!--bindings={"ng-reflect-ng-for-of": "[object Object],[object Object"}-->
            <td></td>
            <td></td>
        </tr>
        <tr>
            <!--bindings={"ng-reflect-ng-for-of": "[object Object],[object Object"}-->
            <td></td>
            <td></td>
        </tr>
        <tr>
            <!--bindings={"ng-reflect-ng-for-of": "[object Object],[object Object"}-->
            <td></td>
            <td></td>
        </tr>
    </tbody>
</table>

But now I am stuck trying to insert the content of the <app-column> component into the <app-table> template. I have read several answers around here (this one and this one). But the issue I have with those is that either you have a static set of templates or you insert them in static places. In my case, I need to use that template inside an *ngFor at runtime.

In AngularJS, I was using the following code:

function compile(tElement) {
    const columnElements = tElement.find('column');
    const columns = columnElements.toArray()
        .map(cEl => ({
            field: cEl.attributes.field.value,
            header: cEl.attributes.header.value,
        }));

    const template = angular.element(require('./smart-table.directive.html'));

    tElement.append(template);

    // The core of the functionality here is that we generate <td> elements and set their
    // content to the exact content of the "smart column" element.
    // during the linking phase, we actually compile the whole template causing those <td> elements
    // to be compiled within the scope of the ng-repeat.
    const tr = template.find('tr[ng-repeat-start]');
    columnElements.toArray()
        .forEach(cEl => {
            const td = angular.element('<td/>')
                .html(cEl.innerHTML);
            tr.append(td);
        });
    const compile = $compile(template);

    // comment out originals
    columnElements.wrap(function () {
        return `<!-- ${this.outerHTML} -->`;
    });

    return function smartTableLink($scope) {
        $scope.vm.columns = columns;
        compile($scope);
    };
}
Eric Liprandi
  • 5,324
  • 2
  • 49
  • 68

2 Answers2

4

Here is what I ended up with, thanks to Meriton's pointers.

As he suggested, I changed my app-column component to a directive instead. That directive must appear on a ng-template element:

@Directive({
    selector: 'ng-template[app-column]',
})
export class ColumnDirective {
    @Input() title: string;
    @ContentChild(TemplateRef) template: TemplateRef<any>;
}

The app-table component became:

@Component({
    selector: 'app-table',
    template: `
    <table>
        <thead>
        <th *ngFor="let column of columns">{{column.title}}</th>
        </thead>
        <tbody>
        <tr *ngFor="let row of data">
            <td *ngFor="let column of columns">
            <ng-container
                [ngTemplateOutlet]="column.template"
                [ngTemplateOutletContext]="{$implicit:row}">
            </ng-container>
            </td>
        </tr>
        </tbody>
    </table>
    `,
})
export class TableComponent {
    @Input() data: any[];
    @ContentChildren(ColumnDirective) columns: QueryList<ColumnDirective>;
}

While at first I was a bit turned off by having to expose ng-template to some of my teammates (not very Angular savvy team yet...), I ended up loving this approach for several reasons. First, the code is still easy to read:

<app-table [data]="data">
    <ng-template app-column title="name" let-person>{{person.name}}</ng-template>
    <ng-template app-column title="age" let-person>{{person.birthday}}</ng-template>
</app-table>

Second, because we have to specify the variable name of the context of the template (the let-person) code above, it allows us to retain the business context. So, if we were to now talk about animals instead of people (on a different page), we can easily write:

<app-table [data]="data">
    <ng-template app-column title="name" let-animal>{{animal.name}}</ng-template>
    <ng-template app-column title="age" let-animal>{{animal.birthday}}</ng-template>
</app-table>

Finally, the implementation totally retains access to the parent component. So, let say we add a year() method that extracts the year from the birthday, on the app-root component, we can use it like:

<app-table [data]="data">
    <ng-template app-column title="name" let-person>{{person.name}}</ng-template>
    <ng-template app-column title="year" let-person>{{year(person.birthday)}}</ng-template>
</app-table>
Eric Liprandi
  • 5,324
  • 2
  • 49
  • 68
2

In Angular, content projection and template instantiation are separate things. That is, content projection projects existing nodes, and you will need a structural directive to create nodes on demand.

That is, column needs to become a structural directive, that passes its template to the parent smart-table, which can then add it to its ViewContainer as often as necessary.

meriton
  • 68,356
  • 14
  • 108
  • 175