Error search ion-searchbar

0

Good night everyone I'm having a problem with a search code and would like help. As you type in the search field, the list changes and works normally, but when you delete what you typed, the list does not return with all the elements. Can someone help me? Thanks

<ion-list>
    <ion-item *ngFor="let topic of cuponsTotais">
      {{ topic.numerocupom }}
    </ion-item>
  </ion-list>
(...), {...}, {...}, {...}, {...}, {...}, {...}, {...} ...}, {...}]   0: {numerocupom: "0620"}   1: {numerocupom: "1109"}   2: {numerocupom: "1441"}   3: {numerocupom: "2810"}   4: {numerocupom: "2827"}   5: {numerocupom: "3141"}   6: {numerocupom: "3260"}   7: {numerocupom: "3715"}   8: {numerocupom: "5104"}   9: {numerocupom: "5613"}  10: {numerocupom: "5632"}  11: {numerocupom: "6037"}   length: 12     proto : Array (0)

    
asked by anonymous 16.08.2018 / 05:31

1 answer

0

How is your method of filtering cuponsTotais ? Put it on so you can try to help. But basically you need to reset the list. The filter should be done over the list in its initial state.

EDIT

By your code, you can see that the problem is that you are filtering the coupons and playing on it, that is, you are changing the value of your source.

The ideal would be a localService or something. But simplifying here, let's say you received the response from an API, and you want to filter that result locally, without having to call the API again.

You can make a copy of the result when you populate the couponsType (one of the ways you can do it, to "lose" the reference of the original object).

this.cuponsFiltrados = JSON.parse(JSON.stringify(this.cuponsTotais));

Use this Filled coupons to assemble your list in html. And your filtering method now would look like this:

getTopics(ev: any) {
    let serVal = ev.target.value; 
    if (serVal && serVal.trim() != '') { 
        this.cuponsFiltrados = this.cuponsTotais.filter((topic) => { 
            return (topic.numerocupom.indexOf(serVal.toLowerCase()) > -1); 
        }) 
    } else {
        this.resetCupons(); 
    }
}

private resetCupons() {
    this.cuponsFiltrados = JSON.parse(JSON.stringify(this.cuponsTotais));
}

Must resolve.

Hugs!

    
17.08.2018 / 19:44