Mat-table Sorting Demo not Working

后端 未结 20 2792
旧巷少年郎
旧巷少年郎 2020-12-02 10:55

I am trying to get the mat-table sorting to work locally, and while I can get the data to show up as expected, clicking on the header row does not do the sortin

20条回答
  •  我在风中等你
    2020-12-02 11:21

    If your table is inside an *ngIf and you think it has something to do with it not sorting your table, then specifying your own sortingDataAccessor function might solve the issue as it did for me. I have my table inside couple of *ngIfs and taking it out of those *ngIfs did not make sense:

    `ngAfterViewInit(): void {
            this.matchesDataSource.sort = this.sort;
            this.matchesDataSource.sortingDataAccessor = previewMatchSortingFn;
        }`
    
    `export function previewMatchSortingFn(item: Match, header: string): string | number {
        switch (header) {
            case 'home':
                return item.homeTeam.name;
            case 'away':
                return item.awayTeam.name;
            case 'date':
                if (item.dateTime) {
                    // this will return the number representation of the date
                    return item.dateTime.valueOf();
                }
                return;
            default:
                break;
        }
    }`
    

提交回复
热议问题