我在通过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原因
<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;
}
我该如何解决?有任何想法吗?谢谢!
答案 0 :(得分:2)
另一种遍历dataSource元素的方式:
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: https://stackblitz.com/edit/angular-mat-table-create-dynamically
答案 1 :(得分:1)
尝试将索引与ngFor一起使用,以跟踪您的迭代。 希望对您有所帮助。
<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>