将链接/模板列添加到自定义表组件

时间:2017-03-03 19:14:59

标签: angular typescript

我有一个基于本文构建的表组件:Creating an Angular2 Datatable from Scratch

我一直在扩展它,所以做我的应用程序需要的不同的东西(如排序和分页),但有一点我无法弄清楚如何提供某种"模板栏"允许创建编辑/删除链接等内容。

我试图找出如何让<ng-content>ColumnComponent中工作,以便我可以通过这种方式传递链接/路由器链接模板,但我不知道如何做到这一点与这个表的构建方式。

以下是我的组件的简化版本:

Plunkr

现在的(简化)组件:

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);
    }
}

现有组件的示例组件标记

<datatable  [dataset]="photoData">
    <column [value]="'id'" [header]="'ID'"></column>
    <column [value]="'title'" [header]="'Title'"></column>
</datatable>

所需的标记示例 不一定非常像这样,但我试图实现以下目标:

<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>

1 个答案:

答案 0 :(得分:4)

我会利用ngTemplateOutlet来实现它。

为您的

创建对可能模板的引用

<强> column.component.ts

@ContentChild('tableHeaderTemplate') headerTemplate: TemplateRef<any>;
@ContentChild('tableBodyTemplate') bodyTemplate: TemplateRef<any>;

因此,如果我们提供了标题或正文,我们现在可以使用自定义模板

<强> datatable.component.html

<table class="table table-striped table-hover">
    <thead>
      <tr>
        <th *ngFor="let col of columns">
           <ng-container *ngIf="!col.headerTemplate">{{col.header}}</ng-container> 
           <ng-template *ngIf="col.headerTemplate" [ngTemplateOutlet]="col.headerTemplate" [ngTemplateOutletContext]="{ $implicit: { header: col.header } }"></ng-template>
        </th>
      </tr>
    </thead>
    <tbody *ngFor="let row of dataset; let i = index">
      <tr>
        <td  *ngFor="let col of columns">
          <ng-container *ngIf="!col.bodyTemplate">{{row[col.value]}}</ng-container> 
          <ng-template *ngIf="col.bodyTemplate" [ngTemplateOutlet]="col.bodyTemplate" [ngTemplateOutletContext]="{ $implicit: { value: row[col.value] }, row: row }"></ng-template>
        </td>
      </tr>
    </tbody>
</table> 

最后表定义可能如下所示:

<datatable  [dataset]="photoData">
    <column [value]="'id'" [header]="'ID'"></column>
    <column [value]="'title'" [header]="'Title'">
        <ng-template #tableHeaderTemplate let-column>
            <span style="color: red">{{ column.header }}</span>
        </ng-template>
    </column>
    <column [value]="'title'" [header]="'Actions'">
      <ng-template #tableBodyTemplate let-column let-row="row">
          <button (click)="remove(row.id)">Remove {{row.id}}</button>
      </ng-template>
    </column>
</datatable>

<强> Plunker Example

相关问题