如何解决为什么角表没有排序?
这是table.component.ts文件。我想对表进行排序,例如在angular的示例中: 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;
})
})
无论如何,Angular会为您排序数据,我无法理解您的函数排序和变量sortedData。您只需要相等的datasource.data即可响应您的API
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。