gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 16:14:53 25 4
gpt4 key购买 nike

我想知道是否可以将 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>

我该如何做这样的事情:
<mat-table #table [dataSource]="dataSource" class="table" matSort>
<user-table-row> </user-table-row>
</mat-table>

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

最佳答案

我创建了一个自定义 TableComponent它使用 @Directiveng-template定义列。这非常灵活,并且抽象了所有 Material 。因此,您可以在整个应用程序中获得通用的表格设计。您只需在此组件中定义所有内容。

我将只包括相关部分。
@Directive用于获取 ng-template 之间的映射和 Material 表列名称。

@Directive({ selector: 'ng-template[rowColumn]' })
export class TableRowColumnDirective {
@Input() rowColumn: string;
constructor(public templateRef : TemplateRef<any>) { }
}
@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);
}
}
@Component的 HTML 只是添加了 mat-header-cellmat-cell使用 *ngFor环形。然后,单元格的内容将使用提供的 ng-template 填充。 s。
<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>

然后可以像这样使用该表:
<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>

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

我知道这不是您要寻找的,但我想您可以看到如何将自定义行注入(inject) mat-table .以此为起点,我敢肯定,您将能够构建适合您需求的定制解决方案。

关于另一个组件中的 Angular 2-4 Mat-Table 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47459673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com