另一个组件中的 Angular 2-4 Mat-Table 行

Ped*_*des 5 xmltable angular-material formattable angular

我想知道是否可以将 mat 表格行放置在与表格组件不同的另一个组件中,这是一个示例:

<mat-table #table [dataSource]="dataSource" class="table" matSort>
<user-table-row> </user-table-row>

  <ng-container matColumnDef="profile">
    <mat-header-cell *matHeaderCellDef mat-sort-header class="header-cell"> Profile Type </mat-header-cell>
    <mat-cell *matCellDef="let user" class="cell"> {{user.profile.type}} </mat-cell>
  </ng-container>

  <ng-container matColumnDef="status">
    <mat-header-cell *matHeaderCellDef mat-sort-header class="header-cell"> Status </mat-header-cell>
    <mat-cell *matCellDef="let user" class="cell"> {{user.profile.status}} </mat-cell>
  </ng-container>


<!-- Header and Row Declarations -->
  <mat-header-row *matHeaderRowDef="['createdAt', 'email', 'profile', 'status', 'isApproved', 'actions']" class="header-row"></mat-header-row>
  <mat-row *matRowDef="let row; columns: ['createdAt', 'email', 'profile', 'status', 'isApproved', 'actions']" class="row"></mat-row>
</mat-table>
Run Code Online (Sandbox Code Playgroud)

我如何制作这样的东西:

<mat-table #table [dataSource]="dataSource" class="table" matSort>
  <user-table-row> </user-table-row>
</mat-table>
Run Code Online (Sandbox Code Playgroud)

可能吗,有人以前做过吗?

Ben*_*n M 6

我创建了一个自定义TableComponent,它使用@Directivewithng-template来定义列。这非常灵活,可以抽象出所有物质。因此,您可以在整个应用程序中获得通用的表设计。你只需要在这个组件中定义一次。

我将只包括相关部分。

@Directive用于获得之间的映射ng-template和材料表列名。

@Directive({ selector: 'ng-template[rowColumn]' })
export class TableRowColumnDirective {
    @Input() rowColumn: string;
    constructor(public templateRef : TemplateRef<any>) { }
}
Run Code Online (Sandbox Code Playgroud)

@Component获取所有ng-templates,建立一个Map访问ng-templates通过他们的列名。

@Component({
    selector: 'my-table',
    templateUrl: './table.component.html'
})
export class TableComponent<T> {
    @ContentChildren(TableRowColumnDirective) rowColumnDirectives: QueryList<TableRowColumnDirective>;

    displayedColumns: ['foo', 'bar'];

    // I use a Map here only, because it's much easier to access the
    // provided ng-templates within table.component.html
    rowColumnTemplates: Map<string, TableRowColumnDirective>;

    ngAfterContentInit() {
        // ArrayUtils.toMap is just a little helper function, which
        // converts an array to a Map. In this case the value of
        // `rowColumn` will become the key of the map
        this.rowColumnTemplates = ArrayUtils.toMap(this.rowColumnDirectives.toArray(), val => val.rowColumn);
    }
}
Run Code Online (Sandbox Code Playgroud)

@Component的HTML简单地增加mat-header-cellmat-cell使用*ngFor循环。然后,单元格的内容将被提供的ng-templates填充。

<mat-table ...>
    <ng-container *ngFor="let column of displayedColumns" [matColumnDef]="column">
        <mat-header-cell *matHeaderCellDef>
            HEADER CONTENT
        </mat-header-cell>
        <mat-cell *matCellDef="let row">
            <ng-container [ngTemplateOutlet]="rowColumnTemplates.get(column).templateRef" [ngTemplateOutletContext]="{$implicit: row}"></ng-container>
        </mat-cell>
    </ng-container>

    <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
    <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
Run Code Online (Sandbox Code Playgroud)

然后可以像这样使用该表:

<my-table>
    <!-- rowColumn matches the displayedColumns array -->
    <ng-template rowColumn="foo" let-row>{{row.profile.type}}</ng-template>
    <ng-template rowColumn="bar" let-customRowName>{{row.profile.status}}</ng-template>
</my-table>
Run Code Online (Sandbox Code Playgroud)

现在您可以@Directive为标题列添加另一个。在我提供的示例中,标题将始终为HEADER CONTENT. 它基本上只是TableRowColumnDirective.

我知道这是不是正是你要找的,但我想你可以看到你是如何注入自定义行插入mat-table。以此为起点,我相信您将能够构建适合您需求的自定义解决方案。