Home > Enterprise >  Angular Material - disable descending sort
Angular Material - disable descending sort

Time:05-31

I have REST API that only supports ascending sort, is it possible to disable descending sort direction in MatTable but keep ascending direction and no sort option?

CodePudding user response:

Does this answer your question?

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.

CodePudding user response:

It's only override the function sort of the MatSort

  @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
  }
  • Related