admin管理员组文章数量:1432359
I have some experience working in web dev but I am very new to Angular. I am trying to create a simple filter to filter one column of a table based on a text input. The problem that I am having is that when you type in a single letter into the text input, all of the results are filtered away.
AnimalsComponent.ts
import { ApiService } from '../api.service';
import { AnimalFilterPipe } from '../animal-filter.pipe'
@Component({
selector: 'app-animals',
templateUrl: './animalsponent.html',
styleUrls: ['./animalsponent.css'],
providers: [AnimalFilterPipe]
})
export class AnimalsComponent implements OnInit {
animals = [];
constructor(private apiService: ApiService) { }
ngOnInit() {
this.apiService.getA().subscribe((data: any[])=>{
console.log(data);
this.animals = data;
})
}
}
Animals Filter Pipe
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'animalFilter'
})
export class AnimalFilterPipe implements PipeTransform {
transform(animals: any, term: string): any {
//check if the search term is defined
if(!animals || !term) return animals;
//return updated animals array
animals.filter(function(animal){
return animal.Animal.toLowerCase().includes(term.toLowerCase());
})
}
}
Animals.html
<div style="padding: 13px;">
<form id = "animalFilter">
<label>Filter by Animal:</label>
<input type="text" [(ngModel)]= "term" [ngModelOptions]="{standalone: true}"/>
</form>
<table>
<tr>
<th>Hemisphere</th>
<th>Type</th>
<th>Animal</th>
<th>Seasonality</th>
<th>Location</th>
<th>Time</th>
<th>Price</th>
</tr>
<tr *ngFor="let animal of animals | animalFilter:term">
<td align="center">{{ animal.Hemisphere }}</td>
<td align="center">{{ animal.Type }}</td>
<td align="center" >{{ animal.Animal }}</td>
<td align="center">{{ animal.Seasonality }}</td>
<td align="center">{{ animal.Location }}</td>
<td align="center">{{ animal.Time }}</td>
<td align="center" *ngIf="animal.Price; else noPrice">{{ animal.Price }} Bells</td>
<ng-template #noPrice>
<td align="center">TBD</td>
</ng-template>
</tr>
</table>
</div>
If anyone could help me and give me some advice about what I need to change and how I can do this better moving forward so that I can create more filter pipes and more custom pipes in general.
I have some experience working in web dev but I am very new to Angular. I am trying to create a simple filter to filter one column of a table based on a text input. The problem that I am having is that when you type in a single letter into the text input, all of the results are filtered away.
AnimalsComponent.ts
import { ApiService } from '../api.service';
import { AnimalFilterPipe } from '../animal-filter.pipe'
@Component({
selector: 'app-animals',
templateUrl: './animals.ponent.html',
styleUrls: ['./animals.ponent.css'],
providers: [AnimalFilterPipe]
})
export class AnimalsComponent implements OnInit {
animals = [];
constructor(private apiService: ApiService) { }
ngOnInit() {
this.apiService.getA().subscribe((data: any[])=>{
console.log(data);
this.animals = data;
})
}
}
Animals Filter Pipe
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'animalFilter'
})
export class AnimalFilterPipe implements PipeTransform {
transform(animals: any, term: string): any {
//check if the search term is defined
if(!animals || !term) return animals;
//return updated animals array
animals.filter(function(animal){
return animal.Animal.toLowerCase().includes(term.toLowerCase());
})
}
}
Animals.html
<div style="padding: 13px;">
<form id = "animalFilter">
<label>Filter by Animal:</label>
<input type="text" [(ngModel)]= "term" [ngModelOptions]="{standalone: true}"/>
</form>
<table>
<tr>
<th>Hemisphere</th>
<th>Type</th>
<th>Animal</th>
<th>Seasonality</th>
<th>Location</th>
<th>Time</th>
<th>Price</th>
</tr>
<tr *ngFor="let animal of animals | animalFilter:term">
<td align="center">{{ animal.Hemisphere }}</td>
<td align="center">{{ animal.Type }}</td>
<td align="center" >{{ animal.Animal }}</td>
<td align="center">{{ animal.Seasonality }}</td>
<td align="center">{{ animal.Location }}</td>
<td align="center">{{ animal.Time }}</td>
<td align="center" *ngIf="animal.Price; else noPrice">{{ animal.Price }} Bells</td>
<ng-template #noPrice>
<td align="center">TBD</td>
</ng-template>
</tr>
</table>
</div>
If anyone could help me and give me some advice about what I need to change and how I can do this better moving forward so that I can create more filter pipes and more custom pipes in general.
Share Improve this question asked Apr 13, 2020 at 12:16 Aidan BakerAidan Baker 572 silver badges8 bronze badges 4- You need code review for this? – Lemmy Commented Apr 13, 2020 at 12:17
- So you need to start filtering when you type multiple letters? Please describe the behavior you want to get – Guerric P Commented Apr 13, 2020 at 12:28
- @GuerricP so the behavior I am expecting to see is that any length of string should reduce the results displayed in the table. For example, I have 10 animals and 6 contain the letter "A", so when I type "A" I expect to see those four animals. Instead, upon even typing a single letter, all of the results are filtered away and the table is empty. – Aidan Baker Commented Apr 13, 2020 at 12:34
- @Lemmy essentially, yes. But, I would like to know fundamentally why my code is not working as expected. – Aidan Baker Commented Apr 13, 2020 at 12:37
1 Answer
Reset to default 4You just forgot a return
statement in the transform
:
return animals.filter(animal => animal.Animal.toLowerCase().includes(term.toLowerCase()));
本文标签: javascriptCreating a Filter Pipe in Angular 8Stack Overflow
版权声明:本文标题:javascript - Creating a Filter Pipe in Angular 8 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745604554a2665762.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论