This commit is contained in:
trochas
2024-10-21 16:38:32 +02:00
parent e503d598ad
commit 7022f766a7
3 changed files with 18 additions and 4 deletions

View File

@@ -5,8 +5,17 @@ import { Pipe, PipeTransform } from '@angular/core';
})
export class FilterPokemonPipePipe implements PipeTransform {
transform(value: unknown, ...args: unknown[]): unknown {
return null;
transform(pokes: any[], property?: string, searchString?: string): any {
if(typeof searchString == 'undefined'){
return pokes;
}
else if (typeof pokes !== 'undefined' && typeof property !== 'undefined') {
return pokes.filter((poke) => {
return poke[property].toLowerCase().indexOf(searchString.toLowerCase()) !== -1;
});
} else {
return [];
}
}
}

View File

@@ -4,10 +4,14 @@
<BR>
id : {{id}}
<BR>
<input [(ngModel)]="searchPokeName">
<BR>
<select [(ngModel)]="selectedPokeId">
<option [value]="poke.id" *ngFor="let poke of pokes"> {{poke.name}}</option>
<option [value]="poke.id"
*ngFor="let poke of pokes | filterPokemonPipe:'name':searchPokeName">
{{poke.name}}</option>
</select>
<ul>
<li *ngFor="let poke of pokes; index as i">{{poke.name}}</li>

View File

@@ -10,6 +10,7 @@ import { Pokemon } from '../pokemon';
export class MyComponentComponent {
id: string;
selectedPokeId : string;
searchPokeName: string;
pokes : Pokemon[] = [];