角材料无法对表格进行排序

时间:2018-10-12 20:51:16

标签: angular typescript sorting material

我尝试使用 MatSortModule 对Angular表中的数据进行排序。问题是排序后的表无法正常工作。 这是代码:

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 =新的MatTableDataSource(orders.result.buy为{} []);

      });
  }
}

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;
}

购买并出售.model.ts

import { QuantityRate } from './quantity-rate.model';

export interface BuyAndSell {
    buy?: QuantityRate;
    sell?: QuantityRate;
}

购买并出售.model.ts

export interface QuantityRate {
    Quantity?: Number;
    Rate?: Number;
}

排序图标显示在标题上,但单击后表格数据不会更改。 我认为问题在于将orders.result.buy转换为表的对象。我在 main.component.ts 中突出显示了MatTableDataSource仅接受{} []格式。有人知道如何解决吗?

1 个答案:

答案 0 :(得分:0)

我不确定“ MatTableDataSource仅接受{} []格式”是什么意思,有很多方法可以将数据传递给MatTableDataSource:

dataSource = new MatTableDataSource();

this.orderBookService
       .getOrderBookBittrex(currency1, currency2)
       .subscribe(
           orders => { this.datasource.data = orders;}

    dataSource = new MatTableDataSource(orders)

还要设置datasource.sort AfterViewInit而不是OnInIt as shown in this stackblitz

相关问题