0

I am trying to sort a mat-table using the mat-sort-header. I am able to do it with common attributes like string or number.

 <table #tablaClientes mat-table [dataSource]="dataSource" matSort multiTemplateDataRows>
  <!-- Id Column -->
  <ng-container matColumnDef="idIngreso">
    <th mat-header-cell *matHeaderCellDef mat-sort-header>Id Comprobante</th>
    <td mat-cell *matCellDef="let row">{{row.idIngreso}}</td>
  </ng-container>
  <!-- Proveedor Column -->
  <ng-container matColumnDef="idProveedor">
    <th mat-header-cell *matHeaderCellDef mat-sort-header>Nombre Proveedor</th>
    <td mat-cell *matCellDef="let row">{{row.idProveedor.nombre}}</td>
  </ng-container>
  <!-- Fecha Compra Column -->
  <ng-container matColumnDef="fechaCompra">
    <th mat-header-cell *matHeaderCellDef mat-sort-header>Fecha de Compra</th>
    <td mat-cell *matCellDef="let row">{{row.fechaCompra}}</td>
  </ng-container>
  <!-- Fecha Recepcion Column -->
  <ng-container matColumnDef="fechaRecepcion">
    <th mat-header-cell *matHeaderCellDef mat-sort-header>Fecha de Recepcion</th>
    <td mat-cell *matCellDef="let row">{{row.fechaRecepcion}}</td>
  </ng-container>
  <!-- Monto Total Column -->
  <ng-container matColumnDef="totalIngreso">
    <th mat-header-cell *matHeaderCellDef mat-sort-header>Monto Total</th>
    <td mat-cell *matCellDef="let row">{{row.totalIngreso |currency}}</td>
  </ng-container>

However I can't sort by idProveedor since it's an object.

Thank you very much!

1 Answer 1

1

The easer way is add to your dataSource a new property and sort by this new property like this SO (really if you don't need the "idProveedor" object, your dataSource can be transform to some like

this.data.forEach(x=>{
   x.proveedorNombre=x.idProveedor.nombre
   delete x.idProveedor
}

Another solution is create a sortingDataAccesor

this.dataSource.sortingDataAccessor = (item, property) => {
     if (property=="idProveedor")
         return item.nombre;
     if (property=="idIngreso")
        return item.idIngreso
     if (property=="fechaCompra")
        return item.fechaCompra

     ...
}

An example simple in this stackblitz

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.