I built a table component following the guidelines from this article: Creating an Angular2 Datatable from Scratch.
While I have added features like sorting and paging to suit my app's needs, I am struggling with implementing a "Template column" to allow for elements like edit/delete links.
I attempted using <ng-content>
within the ColumnComponent
to pass in link/routerlink templates, but couldn't make it work based on how the table is structured.
You can check out a simplified version of my components here: Plunkr
The current (simplified) structure of my components looks like:
datatable.component.html
<table class="table table-striped table-hover">
<thead>
<tr>
<th *ngFor="let column of columns">
{{column.header}}
</th>
</tr>
</thead>
<tbody *ngFor="let row of dataset; let i = index">
<tr>
<td *ngFor="let column of columns">
{{row[column.value]}}
</td>
</tr>
</tbody>
</table>
datatable.component.ts
import { Http, Response } from '@angular/http';
import { Injectable, Component, Input, Output, EventEmitter } from '@angular/core';
import { ColumnComponent } from './column.component';
@Component({
selector: 'datatable',
templateUrl: 'src/datatable.component.html'
})
export class DatatableComponent {
@Input() dataset;
columns: ColumnComponent[] = [];
addColumn(column) {
this.columns.push(column);
}
}
column.component.ts
import {Component, Input} from '@angular/core';
import {DatatableComponent} from './datatable.component';
@Component({
selector: 'column',
template: ``,
})
export class ColumnComponent {
@Input() value: string;
@Input() header: string;
constructor(public table: DatatableComponent) {
table.addColumn(this);
}
}
Example Markup For Existing Components
<datatable [dataset]="photoData">
<column [value]="'id'" [header]="'ID'"></column>
<column [value]="'title'" [header]="'Title'"></column>
</datatable>
Desired Markup Example Although not exact, I'm aiming for something like:
<datatable [dataset]="photoData">
<column [value]="'id'" [header]="Edit">
This is a custom edit link column:
<a [routerLink]="['/edit/', id]">
<span class='glyphicon glyphicon-pencil'></span>
</a>
</column>
<column [value]="'id'" [header]="'ID'"></column>
<column [value]="'title'" [header]="'Title'"></column>
</datatable>