Issue
how i can show n numbers of mattables with diferent data by service if i have currently only one table with one datasource
columns = [
{ columnDef: 'position', header: 'No.', cell: (element: any) => `${element.position}` },
{ columnDef: 'name', header: 'Name', cell: (element: any) => `${element.name}` },
{ columnDef: 'weight', header: 'Weight', cell: (element: any) => `${element.weight}` },
{ columnDef: 'symbol', header: 'Symbol', cell: (element: any) => `${element.symbol}` },
];
displayedColumns = this.columns.map(c => c.columnDef);
dataSource = new ExampleDataSource();
}
const ELEMENT_DATA: any[] = [
{position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
{position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
{position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
{position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
{position: 5, name: 'Boron', weight: 10.811, symbol: 'B'},
{position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'},
{position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
{position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'},
{position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F'},
{position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne'},
{position: 11, name: 'Sodium', weight: 22.9897, symbol: 'Na'},
{position: 12, name: 'Magnesium', weight: 24.305, symbol: 'Mg'},
{position: 13, name: 'Aluminum', weight: 26.9815, symbol: 'Al'},
{position: 14, name: 'Silicon', weight: 28.0855, symbol: 'Si'},
{position: 15, name: 'Phosphorus', weight: 30.9738, symbol: 'P'},
{position: 16, name: 'Sulfur', weight: 32.065, symbol: 'S'},
{position: 17, name: 'Chlorine', weight: 35.453, symbol: 'Cl'},
{position: 18, name: 'Argon', weight: 39.948, symbol: 'Ar'},
{position: 19, name: 'Potassium', weight: 39.0983, symbol: 'K'},
{position: 20, name: 'Calcium', weight: 40.078, symbol: 'Ca'},
];
export class ExampleDataSource extends DataSource<any> {
connect(): Observable<Element[]> {
return Observable.of(ELEMENT_DATA);
}
disconnect() {}
}
in my html component I show the table and the datasource like this
<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>
for example I want to filter by weight and show all those that have the same weight in a table
my expected result
Solution
If your filter params are fixed ,for instance if it's weight, i have tried to edit basic example of mat-table
from docs
@Component({
selector: 'table-basic-example',
styleUrls: ['table-basic-example.css'],
templateUrl: 'table-basic-example.html',
})
export class TableBasicExample implements OnInit {
displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
dataSources:Record<number, PeriodicElement[]>;
ngOnInit() {
this.dataSources = ELEMENT_DATA.reduce((grouped, current) => {
grouped[current.weight] = grouped[current.weight] || [];
grouped[current.weight].push(current);
return grouped;
}, {} as Record<number, PeriodicElement[]>);
}
}
<ng-container *ngFor="let dataSource of dataSources | keyvalue">
<div>Weight: {{dataSource.key}}</div>
<table mat-table [dataSource]="dataSource.value" class="mat-elevation-z8">
<!--- Note that these columns can be defined in any order.
The actual rendered columns are set as a property on the row definition" -->
<!-- Position Column -->
<ng-container matColumnDef="position">
<th mat-header-cell *matHeaderCellDef> No. </th>
<td mat-cell *matCellDef="let element"> {{element.position}} </td>
</ng-container>
<!-- Name Column -->
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef> Name </th>
<td mat-cell *matCellDef="let element"> {{element.name}} </td>
</ng-container>
<!-- Weight Column -->
<ng-container matColumnDef="weight">
<th mat-header-cell *matHeaderCellDef> Weight </th>
<td mat-cell *matCellDef="let element"> {{element.weight}} </td>
</ng-container>
<!-- Symbol Column -->
<ng-container matColumnDef="symbol">
<th mat-header-cell *matHeaderCellDef> Symbol </th>
<td mat-cell *matCellDef="let element"> {{element.symbol}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
</ng-container>
If you have to filter by any other params, create a separate function/s which will try to group the data based on that param.
Also you can change the Weight:
in template to something dynamic if it's required.
Answered By - Amit Kumar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.