[英]Angular Material Unable to sort table
我嘗試使用MatSortModule對 Angular 表中的數據進行排序。 問題是排序表不起作用。 這是代碼:
主模塊.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 {}[]);
});
}
}
主組件.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>
我的服務訂單-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);
}
}
和模型:
訂單簿.model.ts
import { BuyAndSell } from './buy-and-sell.model';
export interface OrderBook {
success?: boolean;
message?: string;
result?: BuyAndSell;
}
買賣模型.ts
import { QuantityRate } from './quantity-rate.model';
export interface BuyAndSell {
buy?: QuantityRate;
sell?: QuantityRate;
}
買賣模型.ts
export interface QuantityRate {
Quantity?: Number;
Rate?: Number;
}
排序圖標顯示在標題上,但單擊后表格數據不會更改。 我認為問題在於將 orders.result.buy 轉換為表的對象。 我在main.component.ts突出,MatTableDataSource只接受{} []格式。 有誰知道如何修復它?
我不確定“MatTableDataSource 只接受 {}[] 格式”是什么意思,有很多方法可以將數據傳遞給 MatTableDataSource:
dataSource = new MatTableDataSource();
this.orderBookService
.getOrderBookBittrex(currency1, currency2)
.subscribe(
orders => { this.datasource.data = orders;}
或者
dataSource = new MatTableDataSource(orders)
還要設置 datasource.sort AfterViewInit 而不是 OnInIt ,如此stackblitz 所示
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.