Default sorting in Angular Material - Sort header

后端 未结 9 1296
执念已碎
执念已碎 2020-12-13 03:14

How can I change Angular Material code below, so that data-table is sorted by \'name\' column, ascending order by default. Arrow (indicating current sort direction) must be

9条回答
  •  醉梦人生
    2020-12-13 03:58

    @ViewChild(MatSort) sort: MatSort;
    
    this.dataSource.sort = this.sort;
    
    const sortState: Sort = {active: 'name', direction: 'desc'};
    this.sort.active = sortState.active;
    this.sort.direction = sortState.direction;
    this.sort.sortChange.emit(sortState);
    

    should work. demo

    And to show sorting direction arrow, add next css (workaround)

    th.mat-header-cell .mat-sort-header-container.mat-sort-header-sorted .mat-sort-header-arrow {
        opacity: 1 !important;
        transform: translateY(0) !important;
    }
    

提交回复
热议问题