Angular Material - 禁用降序

Angular Material - disable descending sort

我有只支持升序排序的 REST API,是否可以在 MatTable 中禁用降序排序方向但保持升序方向并且没有排序选项?

这是否回答了您的问题?

If you want to prevent the user from changing the sorting order of any column, you can use the matSortDisabled binding on the mat-sort, or the disabled on a single mat-sort-header.

From the docs.

只是重写了MatSort的函数sort

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

  ngOnInit()
  {
    this.sort.sort=(sortable: MatSortable)=>{
      if (this.sort.active != sortable.id) {
        this.sort.active = sortable.id;
        this.sort.direction = sortable.start ? sortable.start : this.sort.start;
      } else {
        this.sort.active=''
      }
      
      this.sort.sortChange.emit({active: this.sort.active, direction: this.sort.direction});
    }
    this.dataSource.sort=this.sort
  }