Reputation: 1336
I have a JSON file that contains some runners' data. I'm trying to filter some data by using the following method:
performFilterByRunnerName(filterBy: string):IRunners[]{
return this.runners.filter(x=>x.runnerName=filterBy);
}
Part of the HTML is:
<input #filterinputrunner id="runnerFilter" type="text"
(change) = "performFilterByRunnerName(filterinputrunner.value)"
[ngModel]='listRunnerFilter'>
And I have a list of IRunner
type(interface) that holds the runner's data.
When changing the input
value, what happens is that the runnerName
field changes for all the runners
instead of filtering the runners with the input value.
what am I doing wrong?
Upvotes: 0
Views: 63
Reputation: 39432
If you're using change
it will not update the filtered results as you type, it will only update it on blur
. If you want the list to be updated as you type, you should consider using keyup
.
(keyup) = "performFilterByRunnerName(filterinputrunner.value)"
Also, filter based on the filter search being included in the result and not the exact string search. That would yield better search results:
onFilterChange(filterBy) {
this.filteredUsers = [...this.users.filter(user => user.name.indexOf(filterBy) > -1)];
}
Here's a Sample StackBlitz for your ref.
Upvotes: 1
Reputation: 181
This line of code highlighted is assigning the value not comparing it.
return this.runners.filter(x=>x.runnerName=filterBy)
you need to do a comparison like:
return this.runners.filter(x=>**x.runnerName === filterBy**)
Upvotes: 1
Reputation: 31
Your filter condition seems to be incorrect. You are assigning (=
) the filterBy
property to each of your runners.
What you want is an equality condition (===
).
return this.runners.filter(x => x.runnerName === filterBy);
Upvotes: 1
Reputation: 32507
(x=>x.runnerName=filterBy)
you want to compare not assign
(x=>x.runnerName==filterBy)
should be correct. Voting to close.
Upvotes: 2