Last active
May 6, 2017 11:27
-
-
Save Kamilnaja/cdae6f0a81d54e0cbba5d212e8abcbeb to your computer and use it in GitHub Desktop.
Filter items by the name. Based on Net Ninja Tutorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//inside template | |
<label>Filtruj po rasie</label> | |
<input type="text" name="filter" [(ngModel)]="term"/> | |
</form> | |
<div | |
*ngFor="let dog of dogs | filter:term; | |
let i = index" | |
class="puppy-wrapper" | |
(click)="onSelect(dog)" | |
> | |
//inside filter.pipe.ts (you can generate it with angular cli | |
import { Pipe, PipeTransform } from '@angular/core'; | |
@Pipe({ | |
name: 'filter' | |
}) | |
export class FilterPipe implements PipeTransform { | |
transform(dogs: any, term: any): any { | |
// check if search term is undefined | |
if (term === undefined) { | |
return dogs; | |
} else { | |
return dogs.filter(function(dog){ | |
return dog.name.toLowerCase().includes(term.toLowerCase()); | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment