Skip to content

[Table] Let column sorting be disabled dynamically #7761

@swftvsn

Description

@swftvsn

Bug, feature request, or proposal:

Feature request

What is the expected behavior?

I need to be able to disable sorting under some circumstances on column level. I'd like to do something like [mat-sort-header]="enabled" where enabled is boolean.

What is the current behavior?

I'm not able to dynamically disable sorting

I also experimented with

@ViewChild(MatSort) sort: MatSort
//and later
this.sort.sortables.forEach((value, key) => {
  this.sort.deregister(value)
})

but for some reason this does not do anything when template is set like this:

<mat-table matSort [dataSource]="dataSource" [matSortDisableClear]="true">

    <ng-container matColumnDef="id">
      <mat-header-cell *matHeaderCellDef mat-sort-header> ID </mat-header-cell>
      <mat-cell *matCellDef="let row"> {{row.id}} </mat-cell>
    </ng-container>
.
.
.

The only way to pull this off is to have duplicate columns, one where mat-sort-header is declared and one where it is not. (And toggle the matHeaderRowDef and columns accordingly.)

Metadata

Metadata

Assignees

Labels

P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentfeatureThis issue represents a new feature or feature request rather than a bug or bug fix

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions