如何通过循环将带有按钮的列添加到表中?

时间:2019-08-15 09:51:35

标签: angular angular-material

如何在表格中添加带有按钮的列?

html:

<table mat-table [dataSource]="dataSource">
    <ng-container [matColumnDef]="column.value" *ngFor="let column of allCols;">
        <th mat-header-cell *matHeaderCellDef > {{column.displayName}}</th>
        <td mat-cell *matCellDef="let row"> {{row[column.lowercaseValue]}}</td>
    </ng-container>
    <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
    <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>

这是我的示例表,其中的循环在stackblitz上没有按钮。

2 个答案:

答案 0 :(得分:0)

只需在按钮上添加一个<ng-container>,别忘了将concat添加到displayColumns,该数组具有列的名称。查看代码中的注释

<table mat-table [dataSource]="dataSource">
    <!--- this is our columns-->
    <ng-container [matColumnDef]="column.value" *ngFor="let column of allCols;">
        <th mat-header-cell *matHeaderCellDef > {{column.displayName}}</th>
        <td mat-cell *matCellDef="let row"> {{row[column.lowercaseValue]}}</td>
    </ng-container>
    <!--we add a new column with the matColumnDef="deleteButton"-->
    <ng-container matColumnDef="deleteButton" >
        <th mat-header-cell *matHeaderCellDef > Delete</th>
        <td mat-cell *matCellDef="let row"> <button mat-button>Delete</button></td>
    </ng-container>
    <!--now concat to displayedColumns an array with the buttons
             using .concat(['deleteButton'])
    -->
    <tr mat-header-row *matHeaderRowDef="displayedColumns.concat(['deleteButton'])"></tr>
    <tr mat-row *matRowDef="let row; columns: displayedColumns.concat(['deleteButton']);"></tr>
</table>

查看您的forked stackblitz

注意:我使用concat是因为您的“ displayedColumns”是可变的。如果已解决,则只需在代码中添加新的列名

答案 1 :(得分:0)

一般的想法是,您应该在列的定义中添加一列(例如“操作”),并在模板中使用switch / case条件来区分这些列的类型。

app.component.ts:

import { Component, OnInit } from '@angular/core';
import { MatTableDataSource } from '@angular/material/table';

const COLS = [
  { value: "POSITION", lowercaseValue: "position", displayName: 'Position' },
  { value: "NAME", lowercaseValue: "name", displayName: 'Name' },
  { value: "WEIGHT", lowercaseValue: "weight", displayName: 'Weight' },
  { value: "SYMBOL", lowercaseValue: "symbol", displayName: 'Symbol' },
  { value: "ACTIONS", lowercaseValue: "actions", displayName: 'Actions' },
];

export interface PeriodicElement {
  name: string;
  position: number;
  weight: number;
  symbol: string;
}

const ELEMENT_DATA: PeriodicElement[] = [
  { position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H' },
  { position: 2, name: 'Helium', weight: 4.0026, symbol: 'He' },
  { position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li' },
  { position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be' },
  { position: 5, name: 'Boron', weight: 10.811, symbol: 'B' },
  { position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C' },
  { position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N' },
  { position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O' },
  { position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F' },
  { position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne' },
];

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  displayedCols = COLS;
  allCols = COLS;
  displayedColumns: any[];
  dataSource = new MatTableDataSource(ELEMENT_DATA);
  ngOnInit() {
    this.displayedCols = [
      { value: "NAME", lowercaseValue: "name", displayName: 'Name' },
      { value: "WEIGHT", lowercaseValue: "weight", displayName: 'Weight' },
      { value: "SYMBOL", lowercaseValue: "symbol", displayName: 'Symbol' },
      { value: "ACTIONS", lowercaseValue: "actions", displayName: 'Actions' },
    ];

    this.displayedColumns = this.displayedCols.map(col => col.value);
  }
}

app.component.html:

<mat-form-field>
        <mat-label>Customize Columns</mat-label>
    <mat-select [(ngModel)]="displayedColumns" multiple>
        <mat-option *ngFor="let row of allCols" [value]="row.value">{{ row.displayName }}</mat-option>
    </mat-select>
</mat-form-field>

<table mat-table [dataSource]="dataSource">
    <ng-container [matColumnDef]="column.value" *ngFor="let column of allCols;">
    <ng-container [ngSwitch]="column.value">
      <ng-container *ngSwitchCase="'ACTIONS'">
        <th mat-header-cell *matHeaderCellDef ></th>
        <td mat-cell *matCellDef="let row"><button>Button</button></td>
      </ng-container>

      <ng-container *ngSwitchDefault>
        <th mat-header-cell *matHeaderCellDef > {{column.displayName}}</th>
        <td mat-cell *matCellDef="let row"> {{row[column.lowercaseValue]}}</td>
      </ng-container>
    </ng-container>
    </ng-container>
    <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
    <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>

通过这种方式,您可以处理任何“非常规”列。