html - 迭代 mat-table Angular 中的数据源

标签 html angular typescript angular-material

我在通过 dataSource 进行迭代时遇到问题,其中我有 mat-table 的数据

   <div *ngFor="let element of arrayMain" id = "{{element}}" class="my_item">
      <div><span class="skuska"><span class="mat-subheading-2">{{element}}</span></span></div>
      <mat-table #table [dataSource]="dataSource[1]" matSort>
        <ng-container matColumnDef="{{column.id}}" *ngFor="let column of columnNames">
          <mat-header-cell *matHeaderCellDef mat-sort-header > {{column.value}} </mat-header-cell>
          <mat-cell *matCellDef="let element" [style.background]="element[column.id].color"> <div class="mat-cell-inner">{{element[column.id].name}}</div></mat-cell>

        </ng-container>

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

包含此数据的行包含问题

<mat-table #table [dataSource]="dataSource[1]" matSort>

只有当我更改数组内的数字时才能更改数据:

dataSource[0],dataSource[1]....,dataSource[n]

我尝试使用 *ngFor Cause

<mat-table *ngFor = "let calendars of dataSource" #table [dataSource]="{{calendars}}" matSort>

但它显示错误:

ng: Parser Error: Got interpolation ({{}}) where expression was expected at column 0 in [{{calendars}}] in @101:73

我在函数中填充数据源

for(identificator = 0; identificator < n; identificator++)
       createData(identificator) 

createData(identificator) {
   some code...
       .
       . 
       .
    this.dataSource[identificator] = new MatTableDataSource(testData);
    this.dataSource[identificator].sort = this.sort;

}

我该如何解决这个问题?有任何想法吗?谢谢!

最佳答案

迭代数据源元素的另一种方法:

  dataElement: DataElement[] = [
    {position: 1, name: 'Hy', weight: 1.0079, height: 3.33},
    {position: 2, name: 'He', weight: 4.0026, height: 4.21},
    {position: 3, name: 'Li', weight: 6.941, height: 10.1},
    {position: 4, name: 'Be', weight: 9.0122, height: 11},
    {position: 5, name: 'Bo', weight: 10.811, height: 85},
    {position: 6, name: 'Ca', weight: 12.0107, height: 8.5},
    {position: 7, name: 'Ni', weight: 14.0067, height: 85.1},
    {position: 8, name: 'Ox', weight: 15.9994, height: 0.85},
    {position: 9, name: 'Fl', weight: 18.9984, height: 123},
    {position: 10, name: 'Ne', weight: 2110.1797, height: 8}
  ];

  columns = [
    {columnDef: 'position', header: 'No.', cell: (element: DataElement) => `${element.position}`},
    {columnDef: 'name', header: 'Name', cell: (element: DataElement) => `${element.name}`},
    {columnDef: 'weight', header: 'Weight', cell: (element: DataElement) => `${element.weight}`},
    {columnDef: 'height', header: 'Height', cell: (element: DataElement) => `${element.height}`},
    {columnDef: 'result', header: 'Result', cell: (element: DataElement) => `${element.result}`}
  ];

  displayedColumns = this.columns.map(result => result.columnDef);

  dataSource = this.dataElement;

最后是 html:

<mat-table #table [dataSource]="dataSource">
  <ng-container *ngFor="let column of columns" [cdkColumnDef]="column.columnDef">
    <mat-header-cell *cdkHeaderCellDef>{{ column.header }}</mat-header-cell>
    <mat-cell *cdkCellDef="let row">{{ column.cell(row) }}</mat-cell>
  </ng-container>
  <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
  <mat-row *matRowDef="let row; columns: displayedColumns"></mat-row>
</mat-table>

repo 协议(protocol): https://stackblitz.com/edit/angular-mat-table-create-dynamically

关于html - 迭代 mat-table Angular 中的数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51377884/

相关文章:

javascript - 如何删除使用按钮创建的内容

javascript - 简单js模块的定义文件

html - 标签背景颜色不起作用

Angular2 使 ViewContainerRef 选择器动态化

html - 将绝对 div 放在相对 div 下

Angular 6 - Cordova - 如何将当前的 Angular 项目转换为基于 cordova 的项目?

javascript - 正确的 Promise 处理成功和错误回调

angular - 将变量传递给可观察对象

javascript - JQuery 更改事件未触发

html - 提交表单按钮