ngx-filter-pipe is returning the wrong index of my *ngFor when element is filtered

Asked

Viewed 37 times

0

I have a pipe that performs a filter on my *ngFor:

<input [(ngModel)]="anunciosFiltro.name" type="text" name="filtra" id="filtra">

<tr *ngFor="let anuncio of sortedData | filterBy: anunciosFiltro; let i = index">

TS:

anunciosFiltro: any = { name: '' };

It turns out that inside the *ngFor I have an edit button that uses the index of this *ngFor, when I filter and bring only one product, it takes the index 0, but actually the index of that product in the array sortedData is 4.

Is there any way to get the correct index?

  • Managed to solve?

  • 1

    Yes, I had an edit methodAnuncio(sortedData[i]), changed to editAnuncio(anuncio), it worked

1 answer

0


Since you’re just trying to get the Arr index, you can use the pipe keyvalue! Try the following:

<tr *ngFor="let anuncio of sortedData | keyvalue">

If you want to keep the index order, you can add a small function in your file .ts to do the dirty work;

<tr *ngFor="let anuncio of sortedData | keyvalue:keepOder">

TS

public keepOrder = (a,b) => a.key;

Utilize anuncio.key to recover the desired index!!!

Browser other questions tagged

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