angular - 如何将静态内容添加到我的水平 Angular 垫表中?

标签 angular typescript angular-material angular8 mat-table

我更改了 mat-table 中的方向列,之后我无法将 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>

最佳答案

添加一个 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 的工作示例.

关于angular - 如何将静态内容添加到我的水平 Angular 垫表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57471900/

相关文章:

angular - "error TS2694: Namespace ' angular ' has no exported member ' xxx '"升级后@types/angular

Angular base href 没有出现在 URL 中

javascript - Angular Material 6 Mat-Chip-List - 两个 mat-chip-list 声明共享相同的数据源

angular - 在表格组件内显示 mat-chips

reactjs - HTMLInputElementforwardRef 不接受名称

javascript - TypeScript Store Parse 查询结果

material-design - 使用角 Material 创建一个类似砖石的 md-grid-list

css - 制作一个按钮样式的 Angular 组件( Angular Material )

angularjs - 如何使用 display none 样式隐藏 md-tab

angular - "No provider for TranslateService"错误以某种方式连接到 npm 安装