I am creating a simple component for creating tables:
@Component({
selector: 'admin-table',
template: `
<table class='table table-bordered'>
<thead>
<th *ngFor='let column of columns'>
{{ column.label }}
</th>
</thead>
<tbody>
<tr *ngFor="let record of records">
<td *ngFor='let column of columns' [innerHTML]="fieldContent(column, record) | safeHtml">
</td>
</tr>
</tbody>
</table>
`,
})
export class AdminTableComponent {
@Input() columns: AdminTableColumn[];
@Input() records: {}[];
fieldContent(column: AdminTableColumn, record: {}) {
if (column.template) {
//TODO: parse the template and pass current record as argument
return column.template;
}
return record[column.field];
}
}
and another component for creating a product table using the above component
@Component({
selector: 'product-admin',
template: `
<h1>Products</h1>
<admin-table [columns]="columns" [records]="products"></admin-table>
`,
providers: [ProductService],
})
export class ProductAdminComponent implements OnInit {
products: Product[];
columns: AdminTableColumn[] = [
{
field: 'id',
label: 'SKU',
},
{
field: 'name',
label: 'Name',
template: '<strong>{{record.name}}</strong>',
}
];
}
As you can see, it AdminTableColumnhas an additional parameter templateto set the cell value using the template. But I can not do this when I try to make the value that I received {{record.name}}instead of the real name of the product.
I need to parse the value entered in a parameter templateto allow the use of angular declarations, such as: {{record.name}}or <some-component [title]="record.name"></some-component>, to create a rich table.
In other words, there is something like render(template, { record: record })