微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使Material Table MatSort工作

如何解决如何使Material Table MatSort工作

我对角形材料表不熟悉,只想生成带有过滤器和排序功能的垫子表。遵循一些在线教程之后,大部分内容都可以使用,但是我无法使sort函数正常工作。 代码如下: HTML

<div class="table-header">
    <mat-form-field>
      <input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
    </mat-form-field>
</div>

<div class="table-container mat-elevation-z8">
    
    <mat-table [dataSource]="tableDataSource" matSort>
        
        <ng-container matColumnDef="gene">
            <mat-header-cell *matHeaderCellDef mat-sort-header> Gene </mat-header-cell>
            <mat-cell *matCellDef="let row"> {{row.Gene}} </mat-cell>
        </ng-container>

        <ng-container matColumnDef="gDNAChange">
            <mat-header-cell *matHeaderCellDef mat-sort-header> gDNAChange </mat-header-cell>
            <mat-cell *matCellDef="let row"> {{row.GDnaChange}} </mat-cell>
        </ng-container>

        <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
        <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
    </mat-table>

    <mat-paginator [pageSizeOptions]="[5,10,25,50,100,500]"></mat-paginator>
</div>

打字稿

import { Component,OnInit,ViewChild,AfterViewInit } from '@angular/core';
import { MatPaginator,MatSort,MatTableDataSource } from '@angular/material';
import { GenotypeQueryService } from 'src/app/service/genotype-query.service';
import { GenotypeQueryResult } from 'src/app/model/resultModel/genotype-query-result.model';
   
@Component({
  selector: 'app-genotype-results',templateUrl: './genotype-results.component.html',styleUrls: ['./genotype-results.component.css']
})
export class GenotypeResultsComponent {

  // this is mat table
  tableDataSource: MatTableDataSource<GenotypeQueryResult>;
  displayedColumns: string[] = ['gene','gDNAChange'];

  @ViewChild(MatPaginator,{static: false}) paginator: MatPaginator;
  @ViewChild(MatSort,{static: true}) sort: MatSort;   

  constructor(
    private genoQueryService: GenotypeQueryService{     
      this.tableDataSource = new MatTableDataSource(this.genoQueryService.genotypeQueryList);
  }

  /**
 * Set the paginator and sort after the view init since this component will
 * be able to query its view for the initialized paginator and sort.
 */
  ngAfterViewInit() {
    this.tableDataSource.paginator = this.paginator;
    this.tableDataSource.sort = this.sort;
  }

  applyFilter(filterValue: string) {
    filterValue = filterValue.trim(); // Remove whitespace
    filterValue = filterValue.toLowerCase(); // Datasource defaults to lowercase matches
    this.tableDataSource.filter = filterValue;
  }
}

tableDataSource是对象(GenotypeQueryResult)的数组。

export class GenotypeQueryResult {
       Gene: string
       GDnaChange: string
       RandomPatientId: string
       Omimdisease: string
       Moi: string
       ScreeningMethod: string
       Transcript: string
       Location: string
}

我想念那里吗?谢谢

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。