Skip to content

Instantly share code, notes, and snippets.

@Mustafa-Omran
Created February 21, 2022 10:03
Show Gist options
  • Select an option

  • Save Mustafa-Omran/cf19a58060c393cf0a7a82aca89d803d to your computer and use it in GitHub Desktop.

Select an option

Save Mustafa-Omran/cf19a58060c393cf0a7a82aca89d803d to your computer and use it in GitHub Desktop.
Angular - Filter Tables Or any List using Shared Custom Pipe Filter
<mat-option *ngFor="let value of list | filter:searchKeywords" [value]="value.id" matTooltip="{{value.name}}">
{{value.name}}
</mat-option>
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 { }
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