Reputation: 83
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
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
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
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 themat-sort
, or thedisabled
on a singlemat-sort-header
.
Upvotes: 0