0
votes

Helllo, I need help with something.

I need to show multiple mat-table with different dataSource, I have this in my component.ts

    Groups(){
        this.apiSvc.Cards().subscribe((rsp: any) => { 
            this.groups = rsp;
            this.groups.forEach((row: any) =>{
                this.Employees(row.id);
            });
            
        });
    }
    Employees(id:any){
        this.apiSvc.Groups(id).subscribe((rsp: any) => { 
            this.employees = rsp;
            this.dataSource = new MatTableDataSource(this.employees);
            this.dataSource.paginator = this.paginator;
            console.log(this.dataSource);
        });
    }

And this bring me multiple dataSource like this -

enter image description here

I need like a ngFor for this dataSource that show me multiple tables with different data.

This is my compononent.html

  <div fxFlex.gt-lg="75" fxFlex.gt-md="73" fxFlex.gt-xs="100" fxFlex="100">
    <mat-card class="mat-card-employee">
      <mat-card-content>
        <div class="table-responsive">
          <table mat-table [dataSource]="dataSource" class="table employee-list no-wrap">

            <ng-container matColumnDef="#">
              <th mat-header-cell *matHeaderCellDef> ID </th>
              <td mat-cell *matCellDef="let element"> {{element.id}} </td>
            </ng-container>

            <ng-container matColumnDef="username">
              <th mat-header-cell *matHeaderCellDef> User </th>
              <td mat-cell *matCellDef="let element"> {{element.username}} </td>
            </ng-container>
            
            <ng-container matColumnDef="action">
              <th mat-header-cell *matHeaderCellDef> Actions </th>
              <td mat-cell *matCellDef="let element" class="action-link">
                <a (click)="openDialog('Update',element)" class="m-r-10 cursor-pointer"><i class="fa fa-pencil"></i></a>
                <a (click)="openDialog('Delete',element)" class="m-r-10 cursor-pointer">
                  <i class="fa fa-trash text-danger"></i>
                </a>
              </td>
            </ng-container>

            <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
            <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
          </table>
          <mat-paginator [pageSizeOptions]="[5, 10, 20]" showFirstLastButtons></mat-paginator>
        </div>
      </mat-card-content>
    </mat-card>
  </div>

So if somebody know how I can put multiple dataSource into the [dataSource] property of mat-table I'll appreciate, thanks

1
do you have an unique mat-dataTable or multiple data-table? - Eliseo
Hi, I have a unique mat-dataTable - Alexis
Why don't you use 2 datasource if your tables are separate. - Ammar Hussain
My tables are not separate, is an unique table that I need - Alexis
Your problem is get an unique response with the response to "this.apiSvc.Groups"? then use switchMap and forkJoin - Eliseo

1 Answers

0
votes

I think that your problem is convert multiples clls to an unique with all the "employes", so you can use switchMap and forkJoin to get them, some like:

this.apiSvc.Cards().pipe(
   switchMap((rsp:any[])=>{
     //in rsp we has an array with the "cards"
     //but we want another response using forkJoin
     return forkJoin(
       //create an array of obs transform the before array
       //using map
       rsp.map(x=>this.apiSvc.Groups(x.id))
     )
   })
).subscribe((res:any[])=>{
   ..here you has in res an array with the response to all your "employes"..
   really in "res[0]" you has the response to "this.apiSvc.Groups(rsp[0].id)"
          in "res[1]" you has the response to "this.apiSvc.Groups(rsp[1].id)"
   ....
   this.dataSource = new MatTableDataSource(res);
   this.dataSource.paginator = this.paginator;
})