为什么 angular table 没有排序?

Why angular table didn't sort?

这里是 table.component.ts 文件。我想像 angular 的这个例子一样对 table 进行排序: https://material.angular.io/components/sort/overview 但在本例中,它没有对任何内容进行排序。不知道是什么问题

首先我在 child.module.ts 中导入了模块:

import { MatTableModule } from '@angular/material/table';
import { MatSortModule } from "@angular/material/sort";

导入模块:

    import: [
        MatTableModule,
        MatSortModule
]

这里是 table.component.ts 文件:

 import { MatPaginator } from '@angular/material/paginator';
    import { MatSort } from '@angular/material/sort';
    import { MatTableDataSource } from '@angular/material/table';

    export interface PeriodicElement {
      timestamp: string;  
      key: string;
      value: number;
    
    }
export class TableComponent implements OnInit {

  
  loadingData = false;


  displayedColumns: string[] = ['data', 'key', 'value'];

  ELEMENT_DATA: PeriodicElement[] = new Array<PeriodicElement>();

  dataSource = new MatTableDataSource<PeriodicElement>(this.ELEMENT_DATA);

  datastructure: Array<dataschema> = [];

  sortedData: PeriodicElement[];


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

  constructor(private Service: Service) {
this.sortedData = this.ELEMENT_DATA.slice() }

  exportTable() {
    TableUtil.exportTableToExcel("ExampleTable");
  }


  ngAfterViewInit(): void {
    this.dataSource.sort = this.sort;
    
}


  ngOnInit(): void {
    this.dataSource.paginator = this.paginator;
    this.dataSource.sort = this.sort;

    
    this.loadingData = true;

    this.Service.getContent().then(results => {

      this.datastructure = results;
     
     
      this.loadingData = false;
      
      
      for (let i = 0; i < this.datastructure.length; i++) {

        

        cut off code 
      }

    })

  }

sortData(sort: Sort) {
const data = this.ELEMENT_DATA.slice();
if (!sort.active || sort.direction === '') {
  this.sortedData = data;
  return;
}
this.sortedData = data.sort((a, b) => {
  const isAsc = sort.direction === 'asc';
  switch (sort.active) {
    case 'data': return compare(a.data, b.data, isAsc);
    case 'key': return compare(a.key, b.key, isAsc);
    case 'value': return compare(a.value, b.value, isAsc);
    default: return 0;


   }
    })

    
  }
      
    
}
function compare(a: number | string, b: number | string, isAsc: boolean) {
  return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
}

html 视图:

    <div *ngIf="!loadingData" class="mat-elevation-z8">
       <table id="ExampleTable" mat-table matTableExporter [dataSource]="dataSource"  matSort (matSortChange)="sortData($event)" class="mat-elevation-z8">
<!--Example Column/Row-->



<ng-container matColumnDef="data">
      <th mat-header-cell mat-sort-header *matHeaderCellDef > data </th>
      <td mat-cell *matCellDef="let element"> {{element.data}} </td>
    </ng-container>

    </table>
    </div>

问题出在 ViewChild 中。有错别字。您正在尝试获取 MatSort。但它是 matSort.

TS

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

HTML

<table id="ExampleTable" mat-table matTableExporter [dataSource]="dataSource"  matSort class="mat-elevation-z8">

改为尝试下面的代码,

TS

@ViewChild('exampleTableTableSort, { static: false }) sort: MatSort; 

HTML

<table #exampleTableTableSort='matSort' id="ExampleTable" mat-table matTableExporter [dataSource]="dataSource"  matSort class="mat-elevation-z8">

如果你有*ngIf下的数据Angular无法达到所以我建议给Angular喘口气(附上dataSource.paginator和this.dataSource.sort 在 setTimeout

this.Service.getContent().then(results => {
    this.datastructure = results;
    this.loadingData = false;
    setTimeout(()=>{
      this.dataSource.paginator = this.paginator;
      this.dataSource.sort = this.sort;
    })
})

无论如何,material Angular 为您排序数据,我无法理解您的函数 sort 和您的变量 sortedData。您只需要 datasource.data 等于 API

的响应