Я пытаюсь отсортировать данные в угловой таблице, используя MatSortModule .Проблема в том, что отсортированная таблица не работает.Вот код:
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>
Мой сервис 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);
}
}
И модели:
order-book.model.ts
import { BuyAndSell } from './buy-and-sell.model';
export interface OrderBook {
success?: boolean;
message?: string;
result?: BuyAndSell;
}
купи-продай.модель.тс
import { QuantityRate } from './quantity-rate.model';
export interface BuyAndSell {
buy?: QuantityRate;
sell?: QuantityRate;
}
купи-продай.модель.тс
export interface QuantityRate {
Quantity?: Number;
Rate?: Number;
}
Значки сортировки отображаются в заголовках, но данные таблицы не изменяются после щелчка.Я думаю, что проблема заключается в преобразовании orders.result.buy в объект таблицы.Я выделил в main.component.ts , MatTableDataSource принимает только формат {} [].Кто-нибудь знает, как это исправить?