En mi código, tengo una barra de búsqueda y estoy tratando de filtrar elementos de una lista según lo que he escrito. El filtro funciona bien si busco el elemento en un pedido. Por ejemplo, si busco 'red blue yellow' como 'red blue' pero quiero que el filtro funcione cuando lo busco como 'red yellow' también. Aquí está mi código, ¿qué debo agregar para lograr lo que quiero?
HTML:
<input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">TS:
dataSource: MatTableDataSource<IProduct>; @ViewChild(MatPaginator, { static: true }) paginator: MatPaginator; @ViewChild(MatSort, { static: true }) sort: MatSort; filterPredicate(data: IProduct, filter: string) { let fullText = ""; fullText += data.StockIntegrationCode; fullText += data.ProductGroup ? data.ProductGroup.GroupName : ""; fullText += data.ProductCategory ? data.ProductCategory.CategoryName : ""; fullText += data.ProductName; fullText += data.Description; fullText += data.UnitType ? data.UnitType.Name : ""; fullText += data.IsActive ? "Aktif" : "Taslak"; return (fullText as any).toLocaleLowerCase("tr").indexOf(filter) >= 0; } applyFilter(filterValue: string) { if (this.dataSource) { this.dataSource.filter = filterValue.trim().toLocaleLowerCase(); if (this.dataSource.paginator) { this.dataSource.paginator.firstPage(); } } }Imagina que puedes usar
fullText=fullText.loLocaleLowerCase("tr") return filter.split(' ').filter(x=>(fullText.indexOf(x)<0)).length==0Eso, divida su "filtro", luego tiene, por ejemplo, ["rojo", "amarillo"] y filtra la matriz solo los elementos que NO están en el texto completo.
Si no hay ninguno (longitud==0) es porque todos están en el texto completo.
NOTA: Escribo fullText=fullText.loLocaleLowerCase("tr") para no repetir cada "palabra" en su filtro.