Iterate through dataSource in mat-table Angular

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-10 12:36:06

问题


I have problem with iterations through dataSource, where I have data for 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>

The row with this data contain the problem

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

I can only change the data when i change the number inside the array:

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

I try to use *ngFor cause

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

but it displays an error:

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

I fill the dataSource in function

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

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

}

How can I fix it? Any ideas? Thank you !


回答1:


Try using index with ngFor, to track your iterations. Hope its helpful.

<div *ngFor="let element of arrayMain, let myIndex=index" id = "{{element}}" class="my_item"> <div><span class="skuska"><span class="mat-subheading-2">{{element}}</span></span></div> <mat-table #table [dataSource]="dataSource[myIndex]" matSort> <ng-container matColumnDef="{{column.id}}" *ngFor="let column of columnNames"> <mat-header-cell *matHeaderCellDef mat-sort-header > {{column.value}} </mat-header-cell> // your same code </div>



回答2:


Another way how to iterate through dataSource elements:

  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;

and finally the 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: https://stackblitz.com/edit/angular-mat-table-create-dynamically



来源:https://stackoverflow.com/questions/51377884/iterate-through-datasource-in-mat-table-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!