如何将静态内容添加到我的水平 angular mat-table?

How to add static content to my horizontal angular mat-table?

我更改了 mat-table 中的方向列,之后我无法将 ng-container 添加到 table。我把它做成了 this。在这种情况下如何使用我的内容添加预定义行?

component.ts

export class MyComponent implements OnInit {
    displayedColumns: string[] = ['position', 'name', 'weight', 'symbol', 'actions'];
    dataSource = ELEMENT_DATA;
    fourthFormGroup: FormGroup;
    displayColumns: string[];
    displayData: any[];

    constructor(
        private _formBuilder: FormBuilder
    ) { }

    ngOnInit() {
        this.displayColumns = ['0'].concat(this.dataSource.map(x => x.position.toString()));
        this.displayData = this.displayedColumns.map(x => this.formatInputRow(x));
        this.fourthFormGroup = this._formBuilder.group({});
    }

    formatInputRow(row) {
        const output = {};

        output[0] = row;
        for (let i = 0; i < this.dataSource.length; ++i) {
            output[this.dataSource[i].position] = this.dataSource[i][row];
        }

        return output;
    }
}

component.html

<table mat-table [dataSource]="displayData" class="mat-elevation-z8">

    <!-- Position Column -->
    <ng-container [matColumnDef]="column" *ngFor="let column of displayColumns">
        <th mat-header-cell *matHeaderCellDef> {{ column }} </th>
        <td mat-cell *matCellDef="let element"> {{ element[column] }} </td>
    </ng-container>

    <!-- Below Colunm doesn't show -->
    <ng-container matColumnDef="actions">
        <th mat-header-cell *matHeaderCellDef>Actions </th>
        <td mat-cell *matCellDef="let element">
            <button (click)="accept($event)">Accept</button>
            <button (click)="remove($event)">Remove</button>
        </td>
    </ng-container>

    <tr mat-row *matRowDef="let row; columns: displayColumns"></tr>
</table>

添加一个 ng-container 将添加一个新的 matColumnDef。您不需要列,而是需要显示新行。

因为您只是将 displayColumns 映射为新的 "horizontal" table header。您可以在此处添加 actions 以查看新行。

displayedColumns: string[] = ['position', 'name', 'weight', 'symbol', 'actions'];

现在要显示您的静态内容,我们需要修改您的模板。我们知道 "horizontal" table header 存在于 element[0] 中,我们可以将其与 *ngIf 一起使用以正确显示新行。

<ng-container [matColumnDef]="column" *ngFor="let column of displayColumns">
    <th mat-header-cell *matHeaderCellDef> {{ column }} </th>
    <td mat-cell *matCellDef="let element">
        <span *ngIf="column == 0 || element[0] !== 'actions'; else actions">{{ element[column] }}</span>
        <ng-template #actions>
            <button (click)="accept($event)">Accept</button>
            <button (click)="remove($event)">Remove</button>
        </ng-template>
    </td>
</ng-container>

这是 StackBlitz 上的一个工作示例。

下面的代码对我来说工作正常。

<ng-container [matColumnDef]="column" *ngFor="let column of displayColumns">
    <th mat-header-cell *matHeaderCellDef> {{ column }} </th>
    <td mat-cell *matCellDef="let element">
        <span *ngIf="column!= 'actions'; else actions">{{ element[column] }}</span>
        <ng-template #actions>
            <button (click)="accept($event)">Accept</button>
            <button (click)="remove($event)">Remove</button>
        </ng-template>
    </td>
</ng-container>