I have two tables on the same screen, each one is filled with different datasources and what I want is to add a matSort to each table on all the fields to sort asc or desc
I have it as follows and it works fine in the first table
component.ts
@ViewChild(MatPaginator) paginator!: MatPaginator;
@ViewChild(MatSort) sort!: MatSort;
ngOnInit(): void {
this.obtenerCatalogoProducto();
this.obtenerCatalogoSubProducto();
}
dataSource: any;
obtenerCatalogoProducto() {
this.catService.obtenerCatProd().subscribe((response) => {
this.catProducto = response.data;
console.log('PRUEBA CAT PROD ', response.data);
this.dataSource = new MatTableDataSource(this.catProducto);
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
});
}
html I add matSort in tag and mat-sort-header into tag
<form>
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8" matSort>
<ng-container matColumnDef="idProducto">
<th mat-header-cell *matHeaderCellDef mat-sort-header>ID.</th>
<td mat-cell *matCellDef="let element">{{ element.idProducto }}</td>
</ng-container>
<ng-container matColumnDef="descripcionProducto">
<th mat-header-cell *matHeaderCellDef mat-sort-header>PRODUCTO</th>
<td mat-cell *matCellDef="let element">
<mat-form-field>
<input matInput [value]="element.descripcionProducto" />
</mat-form-field>
</td>
</ng-container>
<ng-container matColumnDef="botonTipoActions">
<th mat-header-cell *matHeaderCellDef> ACCIONES </th>
<td mat-cell *matCellDef="let element">
<button mat-icon-button color="primary">
<mat-icon>edit</mat-icon>
</button>
<button mat-icon-button color="warn" (click)="eliminar($event.target,element)">
<mat-icon>delete</mat-icon>
</button>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns"></tr>
</table>
</form>
<mat-paginator [pageSizeOptions]="[5, 10]" showFirstLastButtons aria-label="Select page of periodic elements">
</mat-paginator>
but when I try to repeat the same on the second table it doesn't work, I did it this way
component.ts
@ViewChild('sortA') sortA!: MatSort;
@ViewChild('sortB') sortB!: MatSort;
ngOnInit(): void {
this.obtenerCatalogoProducto();
this.obtenerCatalogoSubProducto();
}
dataSource: any;
obtenerCatalogoProducto() {
this.catService.obtenerCatProd().subscribe((response) => {
this.catProducto = response.data;
this.dataSource = new MatTableDataSource(this.catProducto);
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sortA;
});
}
dataSourceSubProducto: any;
private obtenerCatalogoSubProducto() {
this.catService.obtenerCatSubProd().subscribe((response) => {
this.catSubProducto = response.data;
this.dataSourceSubProducto = new MatTableDataSource(this.catSubProducto);
this.dataSource.sort = this.sortB;
});
}
second table
<table mat-table [dataSource]="dataSourceSubProducto" class="mat-elevation-z8" matSort>
<ng-container matColumnDef="idSubProducto">
<th mat-header-cell *matHeaderCellDef mat-sort-header>ID.</th>
<td mat-cell *matCellDef="let element">{{ element.idSubProducto }}</td>
</ng-container>
<ng-container matColumnDef="descripcionSubProducto">
<th mat-header-cell *matHeaderCellDef mat-sort-header>SUBPRODUCTO</th>
<td mat-cell *matCellDef="let element">
<mat-form-field>
<input matInput [value]="element.descripcionSubProducto" />
</mat-form-field>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumnsSubProd"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumnsSubProd"></tr>
</table>
but doesnt work, anyone could help me please, what is the way correct
Disclamer I can not found the answer that solve a similar question (but I rememeber someone answer before)
Angular is not "magic". When we use
Angular get the first paginator and the first MatSort that found.
If we have two paginators and/or two MatSort (well, when we have two mat-table) we need use two variables. For this we use a template reference variable in .html
Then we can "reach the mat-sort and the mat-paginator using ViewChild. In viewChild we can indicate "what we are looking for" using "{read:...}", see the docs about using selector. This is necessary to reach the "matSort" (but not to reach the paginator)
We can use `{static:true}, if not it's under a *ngIf
Then we can use as usual