Posts

Showing posts from February, 2023

angular search pipe

  import { Pipe , PipeTransform } from '@angular/core' ; @ Pipe ({   name : 'searchFilter' , }) export class SearchFilterPipe implements PipeTransform {   transform ( items : any [], searchText : string ): any {     if (! items ) {       return [];     }     if (! searchText ) {       return items ;     }     const searchTextLowerCase = searchText . toLocaleLowerCase ();     return items . filter (( item ) => {       return item . name . toLocaleLowerCase (). includes ( searchTextLowerCase );     });   } } in html use like this *ngFor = "let listItem of templateData | searchFilter: searchText"

sorting in angular 14

 based on pipe sorting import { Pipe , PipeTransform } from '@angular/core' ; export type SortOrder = 'asc' | 'desc' ; @ Pipe ({   name : 'tableSorting' }) export class TableSortingPipe implements PipeTransform {         public sortByTab :   any ;   transform ( value : any [], sortOrder : SortOrder | string = 'asc' , sortKey ?: any ): any {     let sortArrayData = value ;       this . sortByTab = ( a : any , b : any ) => {       const isNumber = ( v : any ) => Number ( v ). toString () === v ;       const aPart = a [ sortKey ]. toString (). match ( /\d + | \D + / g );       const bPart = b [ sortKey ]. toString (). match ( /\d + | \D + / g );       let i = 0 ;       const len = Math . min ( aPart . length , bPart . length );       while ( i < len && aPart [ i ] === bPart [ i ]) { ...