I'm attempting to organize data within an Angular table by utilizing the MatSortModule. Unfortunately, the sorted table is not functioning correctly. Below is the provided code:
main.module.ts
import { MatTableModule, MatSortModule } from '@angular/material';
@NgModule({
declarations: [
MainComponent
],
imports: [
CommonModule,
MatTableModule,
MatSortModule,
],
providers: []
})
export class MainModule { }
main.component.ts
import { Component, OnInit, AfterViewInit, ViewChild } from '@angular/core';
import { OrderBook } from '../core/order-book/order-book.model';
import { OrderBookService } from '../core/order-book/order-book.service';
import { MatSort, MatTableDataSource } from '@angular/material';
@Component({
selector: 'app-main',
templateUrl: './main.component.html',
styleUrls: ['./main.component.css']
})
export class MainComponent implements OnInit {
displayedColumns: string[] = ['Quantity', 'Rate'];
orderBook: OrderBook;
dataSource;
constructor(private orderBookService: OrderBookService) { }
@ViewChild(MatSort) sort: MatSort;
ngOnInit() {
this.getTransfers();
}
AfterViewInit() {
this.dataSource.sort = this.sort;
}
private getTransfers(): void {
const currency1 = 'BTC';
const currency2 = 'LTC';
this.orderBookService.getOrderBookBittrex(currency1, currency2)
.subscribe(orders => {
this.orderBook = orders;
this.dataSource = new MatTableDataSource(orders.result.buy as {}[]);
});
}
}
main.component.html
<table mat-table [dataSource]="orderBook?.result.buy" matSort matSortActive="Quantity" matSortDirection="asc" matSortDisableClear class="mat-elevation-z8">
<ng-container matColumnDef="Quantity">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Quantity </th>
<td mat-cell *matCellDef="let element"> {{element.Quantity}} </td>
</ng-container>
<ng-container matColumnDef="Rate">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Rate </th>
<td mat-cell *matCellDef="let element"> {{element.Rate}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
My service order-book.service.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { OrderBook } from './order-book.model';
@Injectable({
providedIn: 'root',
})
export class OrderBookService {
constructor(private httpClient: HttpClient) {
}
getOrderBookBittrex(currency1: string, currency2: string): Observable<OrderBook> {
const url = `/api/getorderbook?market=${currency1}-${currency2}&type=both`;
return this.httpClient.get<OrderBook>(url);
}
}
And the models:
order-book.model.ts
import { BuyAndSell } from './buy-and-sell.model';
export interface OrderBook {
success?: boolean;
message?: string;
result?: BuyAndSell;
}
buy-and-sell.model.ts
import { QuantityRate } from './quantity-rate.model';
export interface BuyAndSell {
buy?: QuantityRate;
sell?: QuantityRate;
}
buy-and-sell.model.ts
export interface QuantityRate {
Quantity?: Number;
Rate?: Number;
}
Although sorting icons are displayed on headers, the table data remains unaltered after clicking. I suspect the issue lies in converting orders.result.buy into an object compatible with the table. As mentioned in main.component.ts, MatTableDataSource only accepts {}[] format. Any suggestions on resolving this?