3zmo
3zmo

Reputation: 83

Angular Material - disable descending sort

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?

Upvotes: 0

Views: 1082

Answers (3)

Andrei Digori
Andrei Digori

Reputation: 1

Or you can just override getNextSortDirection and hardcode it to return always your desired value.

Look at source code: https://github.com/angular/components/blob/main/src/material/sort/sort.ts#L181C3-L181C46

this.sort.getNextSortDirection = () => '';

Thanks to @eliseo

Upvotes: 0

Eliseo
Eliseo

Reputation: 57971

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
  }

Upvotes: 1

Meqwz
Meqwz

Reputation: 1491

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.

Upvotes: 0

Related Questions