src/pipes/search/search.pipe.ts
A custom Angular pipe for filtering an array of objects based on a search string.
Example :// In your component template:
// Assuming 'items' is an array of objects and 'searchTerm' is a string.
<ul>
<li *ngFor="let item of items | search: searchTerm">{{ item.name }}</li>
</ul>
Name | search |
transform | ||||||||||||
transform(value: any, search: string)
|
||||||||||||
Defined in src/pipes/search/search.pipe.ts:27
|
||||||||||||
Transforms an array of objects by filtering based on a search string.
Parameters :
Returns :
any
|
import { Pipe, PipeTransform } from '@angular/core'
/**
* A custom Angular pipe for filtering an array of objects based on a search string.
*
* @example
* // In your component template:
* // Assuming 'items' is an array of objects and 'searchTerm' is a string.
* <ul>
* <li *ngFor="let item of items | search: searchTerm">{{ item.name }}</li>
* </ul>
*
* @param {any[]} value - The array of objects to filter.
* @param {string} search - The search string to filter by.
* @returns {any[]} - The filtered array of objects.
*/
@Pipe({ name: 'search' })
export class SearchPipe implements PipeTransform {
/**
* Transforms an array of objects by filtering based on a search string.
*
* @param {any[]} value - The array of objects to filter.
* @param {string} search - The search string to filter by.
* @returns {any[]} - The filtered array of objects.
*/
transform(value: any, search: string): any {
if (!search) {
return value
}
if (!value) {
return ''
}
const solution = value?.filter((v: any) => {
if (!v) {
return false
}
return JSON.stringify(v).toLowerCase().indexOf(search.toLowerCase()) > -1
})
return solution
}
}