如何在水平角垫桌上添加静态内容?

时间:2019-08-13 05:58:06

标签: angular typescript angular-material angular8 mat-table

我更改了mat-table中的direction列,之后我无法将ng-container添加到该表中。我像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>

2 个答案:

答案 0 :(得分:6)

添加ng-container将添加新的matColumnDef。您不希望使用列,而是需要显示新行。

由于您只是将displayColumns映射为新的“水平”表标题。您只需在此处添加actions即可看到新行。

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

现在要显示您的静态内容,我们将需要修改您的模板。我们知道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上的一个有效示例。

答案 1 :(得分:0)

以下代码对我来说很好。

.actionSheet(isPresented: $viewModel.isCustomItemSelected) {
        ActionSheet(
            title: Text("Add Item"),
            message: Text("Wich item would you like to add?"),
            buttons: [
                .default(Text("Todo")),
                .default(Text("Event")),
                .cancel(Text("Cancel"))
        ])
}