Created
February 21, 2022 10:03
-
-
Save Mustafa-Omran/cf19a58060c393cf0a7a82aca89d803d to your computer and use it in GitHub Desktop.
Angular - Filter Tables Or any List using Shared Custom Pipe Filter
This file contains hidden or 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
| <mat-option *ngFor="let value of list | filter:searchKeywords" [value]="value.id" matTooltip="{{value.name}}"> | |
| {{value.name}} | |
| </mat-option> |
This file contains hidden or 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
| import { NgModule } from '@angular/core'; | |
| import { CommonModule } from '@angular/common'; | |
| import { FilterPipe } from './filter.pipe'; | |
| @NgModule({ | |
| declarations: [ | |
| FilterPipe | |
| ], | |
| imports: [ | |
| CommonModule | |
| ], | |
| exports: [ | |
| FilterPipe | |
| ] | |
| }) | |
| export class FilterModule { } |
This file contains hidden or 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
| import { Pipe, PipeTransform } from '@angular/core'; | |
| @Pipe({ | |
| name: 'filter' | |
| }) | |
| export class FilterPipe implements PipeTransform { | |
| /** | |
| * filter array of strings | |
| * | |
| * | |
| * @param items | |
| * @param searchText | |
| */ | |
| transform(items: any[], searchText: string): any[] { | |
| if (!items) { | |
| return []; | |
| } | |
| if (!searchText) { | |
| return items; | |
| } | |
| const result = items.filter(item => { | |
| if (item && item.name) { | |
| return item.name.toLowerCase().includes(searchText.toLowerCase()); | |
| } else if (item) { | |
| return item.toLowerCase().includes(searchText.toLowerCase()); | |
| } | |
| return false; | |
| }); | |
| return result; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment