Hello I using Angular Material and I trying to show in table data with following format:
{
"error": [],
"result": {
"ADAUSD": {
"asks": [
[
"0.068890",
"22428.508",
1540845036
],
[...]
In asks table there is another colection of tables. I need to use only first two values of this indented table. In this case 0.068890 and 22428.508.
So I got two column I named it Rate and Quantity. Data is printing properly, but data is sorting only by last column.
Because in this JSON data format there is no objects and only tables it makes many problems.
Here is the component HTML:
<table mat-table [dataSource]="dataSource" matSort (matSortChange)="sortData($event)" matSortActive="Quantity" matSortDirection="asc" matSortDisableClear class="mat-elevation-z8">
<ng-container matColumnDef="Quantity">
<th mat-header-cell *matHeaderCellDef mat-sort-header="Quantity"> Quantity </th>
<td mat-cell *matCellDef="let element"> {{element[1]}} </td>
</ng-container>
<ng-container matColumnDef="Rate">
<th mat-header-cell *matHeaderCellDef mat-sort-header="Rate"> Rate </th>
<td mat-cell *matCellDef="let element"> {{element[0]}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
<mat-paginator [pageSizeOptions]="[5]"></mat-paginator>
And component TS:
import { Component, OnInit, AfterViewInit, ViewChild } from '@angular/core';
import { MatSort, MatPaginator, MatTableDataSource, Sort} from '@angular/material';
import { OrderBookKrakenService } from 'src/app/core/order-book-kraken/order-book-kraken.service';
@Component({
selector: 'app-kraken-sell-usd-ada',
templateUrl: './kraken-sell-usd-ada.component.html',
styleUrls: ['./kraken-sell-usd-ada.component.css']
})
export class KrakenSellUsdAdaComponent implements OnInit, AfterViewInit {
displayedColumns: string[] = ['Quantity', 'Rate'];
dataSource;
sortedData;
@ViewChild(MatSort) sort: MatSort;
@ViewChild(MatPaginator) paginator: MatPaginator;
constructor(private orderBookService: OrderBookKrakenService) {
this.getTransfers();
}
ngOnInit() {
}
ngAfterViewInit() {
}
private getTransfers(): void {
const currency1 = 'ADA';
const currency2 = 'USD';
this.orderBookService.getOrderBookKraken(currency1, currency2)
.subscribe(orders => {
if (!orders) { return; }
console.log(orders);
this.dataSource = orders.result.ADAUSD.bids;
this.dataSource = this.dataSource.slice();
//this.dataSource.sort = this.sort;
this.sortedData.paginator = this.paginator;
});
}
sortData(sort: Sort) {
const data = this.dataSource.slice();
if (!sort.active || sort.direction === '') {
this.dataSource = data;
return;
}
this.dataSource = data.sort((a, b) => {
const isAsc = sort.direction === 'asc';
switch (sort.active) {
case 'Quantity': return compare(a, b, isAsc);
case 'Rate': return compare(a, b, isAsc);
default: return 0;
}
});
}
}
function compare(a: number | string, b: number | string, isAsc: boolean) {
return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
}
As you can see I tried to combine mat-table and mat-sort-header. Unfortunately mat-table sorting is not working at all.