I try to sort data in Angular table using MatSortModule . The problem is that sorted table is not working. Here is the 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;
}
Sorting icons are displayed on headers but table data doesn't change after click. I think the problem is with converting orders.result.buy to object of table. I highlighted in main.component.ts , MatTableDataSource accepts only {}[] format. Anybody knows how to fix it?
I'm not sure what you mean by "MatTableDataSource accepts only {}[] format", there's lots of ways to pass data to MatTableDataSource:
dataSource = new MatTableDataSource();
this.orderBookService
.getOrderBookBittrex(currency1, currency2)
.subscribe(
orders => { this.datasource.data = orders;}
Or
dataSource = new MatTableDataSource(orders)
Also set the datasource.sort AfterViewInit instead of OnInIt as shown in this stackblitz
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.