angular - 在 mat-dialog 中显示 mat-table

标签 angular angular-material angular8 angular-material-7

当我在 mat-dialog 中使用 mat-table 时,我无法显示 mat-table 内容。我可以在 DOM 中看到正确数量的“tr”,但它们是空的。

你能告诉我我做错了什么吗??

.html

<table mat-table [dataSource]="versions[versionIndex].datas" >

      <ng-container matColumnDef="feature">
        <th mat-header-cell *matHeaderCellDef> col 1 </th>
        <td mat-cell *matCellDef="let element"><span>{{element.feature}}</span></td>
      </ng-container>

      <ng-container matColumnDef="version">
        <th mat-header-cell *matHeaderCellDef width="100px">col 2</th>
        <td mat-cell *matCellDef="let element" width="100px"><span>{{element.version}}</span></td>
      </ng-container>

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

.ts
 @Component({selector: 'dialog-overview-example-dialog',
  templateUrl: 'dialog-overview-example-dialog.html',
})
export class DialogOverviewExampleDialog implements OnInit {
  public versions: any[] = [];
  public versionIndex: number = 0;
  constructor(
    public dialogRef: MatDialogRef<DialogOverviewExampleDialog>,
    @Inject(MAT_DIALOG_DATA) public data: DialogData) {}

  onNoClick(): void {
    this.dialogRef.close();
  }

ngOnInit(){
      var toAdd = {
        num: "1",
        date: "01/10/2019",
        datas: []
      };
      toAdd.datas.push({
        feature:"feature 1",
        version:""
      }); 
      toAdd.datas.push({
        feature:"feature 2",
        version:"0.2"
      }); 
      toAdd.datas.push({
        feature:"feature 3",
        version:"0.3"
      }); 
      toAdd.datas.push({
        feature:"feature 4",
        version:""
      }); 
      this.versions.push(toAdd);

  }
}

https://stackblitz.com/edit/angular-q25pg9

result

最佳答案

添加 displayedColumns: string[] = ['feature', 'version'];到您的对话框组件。

@Component({
  selector: 'dialog-overview-example-dialog',
  templateUrl: 'dialog-overview-example-dialog.html',
})
export class DialogOverviewExampleDialog implements OnInit {
  public versions: any[] = [];
  public versionIndex: number = 0;
  displayedColumns: string[] = ['feature', 'version'];

关于angular - 在 mat-dialog 中显示 mat-table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56835697/

相关文章:

javascript - Angular:显示两个类组件之间的差异

angular - 如何动态传递数据到 Material 表数据源

javascript - 如何在 Angular 中用逗号替换换行符

node.js - 添加或更新在spurCommerce中不起作用的产品

unit-testing - 如何在测试中模拟 NavParams?

javascript - Angular Material 1/md-virtual-repeat 选择滚动

angularjs - Angular svg 图标大小

angular - 如何使用ngrx启用trace和traceLimit?

javascript - 调用(this.dragger)不起作用,d3.drag()

javascript - Angular 2 : Programmatically to position inside div overlay