lironzaa
lironzaa

Reputation: 544

Angular filtering table results not working properly

I have a table in Angular which i'm filtering. In ngOnInit I fetch the data and save it to a local variables in order to not make another call for the DB in order to get the data.

Here's the code of the search method:

search() {
    if (this.searched.length > 0) {
      this.history = this.history.filter(res => {
        return res.DestinationCompany.match(this.searched) || res.OriginCompany.match(this.searched) || res.AuctionSerialNumber.match(this.searched) || res.AuctionSerialNumber.match(this.searched);
      })
    } else {
      this.history = this.historyOrigin;
    }
  }

The problem is that when I'm deleting a character the results doesn't update even if they can show more results. For example if I'm searching the expression '7' i get 5 results, if I'm searching the expression '71' iIget 1 result but if now I'm searching '7' again I get 1 result also until I delete all the characters and get back the full results.

How to get more results when i'm deleting a character

Upvotes: 0

Views: 238

Answers (1)

JB Nizet
JB Nizet

Reputation: 691685

When you change the search filter, you filter the data that has already been filtered by the previous search.

Don't do that. Keep a copy of the original, complete list of data, and each time you search, created a filtered copy of that original list, stored in a different variable.

In short, replace

this.history = this.history.filter(...)

by

this.history = this.historyOrigin.filter(...)

Upvotes: 1

Related Questions