我制作了一个简单的组件来创建表:
@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];
}
}
和其他组件使用上述组件创建产品表
@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>',
}
];
}
就像你可以看到的AdminTableColumn
有一个附加选项称为template
使用模板设置单元格的值。但是当尝试渲染我得到的值时我无法做到这一点{{record.name}}
而不是真实的产品名称。
我需要解析输入的值template
允许使用角度声明的选项,例如:{{record.name}}
or <some-component [title]="record.name"></some-component
> 以创建丰富的表。
换句话说,存在类似的东西render(template, { record: record })