--------- .ts file ---------
export interface PeriodicElement {
position: number,
profileImg: string;
name: string;
}
// this is to get reference of sort table components
@ViewChildren(MatSort) sort = new QueryList<MatSort>();
ngAfterViewInit() {
// this is to control all sort elements
this.allDataSource.forEach((dataSource, index) => {
dataSource.sort = this.sort.toArray()[index];
});
}
public configStudentsList: any[] = [
{
id: 1,
studentAmount: '3',
date: '03/04/2020 1:43:43 pm',
dataSource: [
{ position: 1, profileImg: "https://bolavip.com/__export/1580908233923/sites/bolavip/img/2020/02/05/messi-barcelona_crop1580908232023.jpg_1902800913.jpg", name: 'Hydrogen' },
{ position: 2, profileImg: "https://bolavip.com/__export/1580908233923/sites/bolavip/img/2020/02/05/messi-barcelona_crop1580908232023.jpg_1902800913.jpg", name: 'Helium' },
{ position: 3, profileImg: "https://bolavip.com/__export/1580908233923/sites/bolavip/img/2020/02/05/messi-barcelona_crop1580908232023.jpg_1902800913.jpg", name: 'Lithium' }
]
},
{
id: 2,
studentAmount: '11',
date: '30/05/2020 13:43:43 pm',
dataSource: [
{ position: 1, profileImg: "https://thumbs.dreamstime.com/b/el-ejemplo-del-vector-de-disney-mickey-mouse-aisl%C3%B3-en-fondo-blanco-134953070.jpg", name: 'Pedro' },
{ position: 2, profileImg: "https://thumbs.dreamstime.com/b/el-ejemplo-del-vector-de-disney-mickey-mouse-aisl%C3%B3-en-fondo-blanco-134953070.jpg", name: 'ernesto' }
]
}, {
id: 3,
studentAmount: '9',
date: '12/04/2020 2:43:43 pm',
dataSource: [
{ position: 2, profileImg: "https://www.lainformacion.com/files/article_default_content/uploads/2019/02/16/5c683ab9757a9.jpeg", name: 'lisa' },
{ position: 3, profileImg: "https://www.lainformacion.com/files/article_default_content/uploads/2019/02/16/5c683ab9757a9.jpeg", name: 'luis' },
{ position: 4, profileImg: "https://www.lainformacion.com/files/article_default_content/uploads/2019/02/16/5c683ab9757a9.jpeg", name: 'julian' }
]
}
];
allDataSource: any = [];
ngOnInit(): void {
//This is to manipulate all sort elements into (ngfor)
let tableD: PeriodicElement[] = [];
this.configStudentsList.forEach((myObject, index) => {
let tableD: PeriodicElement[] = [];
myObject.dataSource.forEach((source, index) => {
tableD.splice(index, 0, source);
});
//for each table initialize a new MatTableDataSource object
this.allDataSource.splice(index, 0, new MatTableDataSource(tableD));
});
}
--------- html - adapt to your code but take at look of comments -----------
<!--Manipulate tables inside ngFor loop -->
<mat-accordion class="col-md-12 col-lg-12" *ngFor="let config of configStudentsList; let i = index">
<mat-expansion-panel class="expansionCard" [disabled]="true" #mep="matExpansionPanel">
<mat-expansion-panel-header class="expansionHeader">
<mat-panel-title> </mat-panel-title>
<mat-panel-description></mat-panel-description>
</mat-expansion-panel-header>
<!-- this is the data source reference from .ts file-->
<table mat-table [dataSource]=allDataSource[i] matSort #MatSorteds matSortStart="desc" class=" tableStudents">
<ng-container matColumnDef="name">
<td mat-cell *matCellDef="let element">
<img class="profileTableImage" src="{{element.profileImg}} "
alt="https://corgascience.org/wp-content/uploads/2018/03/profil2.png" />
{{element.name | uppercase}}
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr class="trPadding" mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
</mat-expansion-panel>
</mat-accordion>