简体   繁体   English

Angular 15 从 json 数组中过滤多个值,用逗号分隔

[英]Angular 15 filter multiple value from json array seperated by comma

i'm using Angular 15 in my project.我在我的项目中使用 Angular 15。 I'm able to filter single value from search box but what I want to achieve is to filter multiple values from search box seperated by comma.我能够从搜索框中过滤单个值,但我想要实现的是从搜索框中过滤多个值,用逗号分隔。 Any help would be appreciated.任何帮助,将不胜感激。

FilterPipe
import { Pipe, PipeTransform } from '@angular/core';
import { FilterData} from './data';

@Pipe({ name: 'datum' })
export class filterPipe implements PipeTransform {
transform(values: FilterData[], filter: any): FilterData[] {
if (!filter || filter.length === 0) {
  return values;
}

if (values.length === 0) {
  return values;
}

// @ts-ignore
return values.filter((value: Yafo) => {

  const matchFilter = [];
  const filterArray = filter.split(';');
  const columns = (<any>Object).values(values);
  

  // Main
  filterArray.forEach((filter:any) => {
    const customFilter: any = [];
    columns.forEach((column:any) =>
      customFilter.push(column.toLowerCase().includes(filter))
    );
    console.log(customFilter);
    matchFilter.push(customFilter.some(Boolean)); // OR
  });



  const nameFound =
    value.Firma.trim().toLowerCase().indexOf(filter.toLowerCase().split(',')) !== -1;
  const emailFound =
    value.Email.trim().toLowerCase().indexOf(filter.toLowerCase().split(',')) !== -1;
  const plzFound =
    value.Plz.trim().toLowerCase().indexOf(filter.toLowerCase().split(',')) !== -1;
  const datumFound =
    value.RegistrationDatum.trim().toLowerCase().indexOf(filter.toLowerCase().split(',')) !== -1;

  if (nameFound || emailFound || plzFound || datumFound) {
    return value;
  }
});
}
}

I hope, I understand you correctly, you need to split filtered value and add the value to customFilter, like this我希望,我对你的理解是正确的,你需要拆分过滤值并将值添加到 customFilter,就像这样

    this.dataSourceCommaSepareted.filterPredicate = (
          data: UserData,
          filters: string
        ) => {
          const matchFilter = [];
          const filterArray = filters.split(';');
          const columns = (<any>Object).values(data);
          // OR be more specific [data.name, data.race, data.color];
    
          // Main
          filterArray.forEach((filter) => {
            const customFilter = [];
            columns.forEach((column) =>
// Here we split the filter value
              filter
                .split(',')
                .forEach((valueOfFilter) =>
                  customFilter.push(column.toLowerCase().includes(valueOfFilter))
                )
            );
            matchFilter.push(customFilter.some(Boolean)); // OR
          });
          return matchFilter.every(Boolean); // AND
        };

Here is my exapmle on stackblitz https://stackblitz.com/edit/sb-issue-multiple-filter-ygaowm?file=app/app.component.html这是我在 stackblitz 上的例子https://stackblitz.com/edit/sb-issue-multiple-filter-ygaowm?file=app/app.component.html

Here is a solution i did.这是我做的一个解决方案。 It is working.这是工作。 Thanks for the support感谢您的支持

import { Pipe, PipeTransform } from '@angular/core';
import { FilterData} from './data';

@Pipe({ name: 'filter' })
export class filterPipe implements PipeTransform {
  transform(items: any[], filterValues: string): any[]{
  console.log('Filter pipe called');

if(!items || !filterValues){
  return items;
}
const keywords = filterValues.split(',');
return items.filter(item => {
  for (const keyword of keywords) {
    if (item.Firma.toLowerCase().indexOf(keyword.toLowerCase().trim()) !== -1 ||
      item.Plz.toLowerCase().indexOf(keyword.toLowerCase().trim()) !== -1) {
      return true;
    }
    }
   return false;
   })
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM