Last active
January 25, 2021 17:29
-
-
Save AliwerErgashev/30bc5424c0824567a92880d3c3a71b49 to your computer and use it in GitHub Desktop.
Angular Pipes Filtering on Multiple Keys
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
<ul> | |
<li *ngFor="let item of _items | filter:{ label: filterText, description: filterText } : false">{{ item.value }} - {{ item.label }} - {{ item.description }}</li> | |
</ul> | |
@Pipe({ | |
name: 'filter' | |
}) | |
export class FilterPipe implements PipeTransform { | |
transform(items: any, filter: any, isAnd: bool): any { | |
if (filter && Array.isArray(items)) { | |
let filterKeys = Object.keys(filter); | |
if (isAnd) { | |
return items.filter(item => | |
filterKeys.reduce((memo, keyName) => | |
(memo && new RegExp(filter[keyName], 'gi').test(item[keyName])) || filter[keyName] === "", true)); | |
} else { | |
return items.filter(item => { | |
return filterKeys.some((keyName) => { | |
console.log(keyName); | |
return new RegExp(filter[keyName], 'gi').test(item[keyName]) || filter[keyName] === ""; | |
}); | |
}); | |
} | |
} else { | |
return items; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Gracias por tu aporte. Me ayudo en parte sobre mi proyecto a la hora de filtrar los datos. EL tema es que no logro solucionar cuando filtro un dato que no está en la pagina seleccionada. Al hacer se blanquea la pantalla. Mi servidor solo muestra 10 items por pagina, probablemente es donde se origina el inconveniente.
Saludos