Skip to content

[sortable] Sorting does not work in some fields (in fields that show the value of a nested class) #13241

@juanjinario

Description

@juanjinario

I have the following DATA:
const ELEMENT_DATA: Element[] = [
{position: 1, company:{id:1, name:"fox"}},
{position: 2, company:{id:2, name:"mercadona"}},
];

I show it using <mat-table [dataSource]="dataSource" matSort>

<ng-container matColumnDef="position">
  <mat-header-cell *matHeaderCellDef mat-sort-header> No. </mat-header-cell>
  <mat-cell *matCellDef="let element"> {{element.position}} </mat-cell>
</ng-container>

<!-- Company Column -->
<ng-container matColumnDef="company">
  <mat-header-cell *matHeaderCellDef mat-sort-header> Name </mat-header-cell>
  <mat-cell *matCellDef="let element"> {{element.**company.name**}} </mat-cell>
</ng-container>

and in the TS I place the displayedCOlum correctly
displayedColumns = ['position', 'company'];

I can sort the Position field, but not Company field

I know that the matColumnDef must be called the same as the field of the class, but in this case it is impossible for me because the field I show on the screen is called COMPANY.NAME, what is the solution to this case?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions