Client side filtering of a Datasource

Asked

Viewed 30 times

0

I am developing a query of a mass of data from my server, with the purpose of feeding a Datatable (in this case, the Angular Material). I am using the CDK library’s Collectionviewer and Datasource.

In the answer I receive from the API it contains two fields that I need to use for filtering, being the fields "Canceled" and "Signed". My need is to be able to display Signed or Unsigned if the item meets one of the two conditions, or to display Cancelled if cancelled, regardless of whether it was signed or not.

I managed to do the display part using the following code:

const arrayItens: ItensDto[] = response['content'];
    for (const item of arrayItens) {
      if (item.cancelado) {
        item.status = 'Cancelado';
      } else if (item.assinado) {
        item.status = 'Assinado';
      } else {
        item.status = 'Não assinado';
      }
    }

<ng-container matColumnDef="status">
<mat-header-cell *matHeaderCellDef>Status</mat-header-cell>
<mat-cell class="statusCell" *matCellDef="let item">
    <mat-chip-list>
        <mat-chip *ngIf="item.status === 'Cancelado'">Cancelado</mat-chip>
        <mat-chip *ngIf="item.status === 'Não assinado'" selected color="warn">Não assinado</mat-chip>
        <mat-chip *ngIf="item.status === 'Assinado'" selected>Assinado</mat-chip>
    </mat-chip-list>
</mat-cell>

For display purposes, I can differentiate between conditions very well in the datatable, but my main goal is to be able to filter this data in the client side, since the "status" field does not exist in the answer.

I’m open to suggestions too, about better ways to achieve what I need.

  • face a look at the function of array map and filter.

  • o for is almost never used on js

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.