在Angular中制作动态滤镜

时间:2018-11-07 15:34:16

标签: arrays angular filter config

我有一个管道,可以在各个组件之间重用。通常在搜索时。

HTML看起来像这样,您可以看到我有一个带有“ plantNumber”和“ shortDescription”的数组,但是它可能是无尽的属性列表

*ngFor="let workOrder of workOrders | filterArrayPipe: ['plantNumber', 'shortDescription']: searchFilter"

过滤器看起来像这样

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

@Pipe({
  name: 'filterArrayPipe'
})
export class FilterArrayPipe implements PipeTransform {
  transform(value: any, config: any, q: string) {
    if (config && q) {
      return value.filter(result => {
        return result[config[0]].toString().toLowerCase().indexOf(q) > -1 
          || result[config[1]].toString().toLowerCase().indexOf(q) > -1;
      });
    } else {
      return value;
    }
  }
}

但是我希望它看起来更像这样

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

@Pipe({
  name: 'filterArrayPipe'
})
export class FilterArrayPipe implements PipeTransform {
  transform(value: any, config: any, q: string) {
    if (config && q) {
      return value.filter(result => {
        for (let i = 0; i < config.length; i ++) {
          const type = config[i];
          return result[type].toString().toLowerCase().indexOf(q) > -1;
        }
      });
    } else {
      return value;
    }
  }
}

所以问题是,如何添加“和” ||?在return语句中?

1 个答案:

答案 0 :(得分:1)

由于在该... 'postVarSets' => [ '_DEFAULT' => [ 'gallery' => [ [ 'GETvar' => 'tx_myext_p1gallery[action]', 'noMatch' => 'bypass', ], [ 'GETvar' => 'tx_myext_p1gallery[controller]', 'noMatch' => 'bypass', ], [ 'GETvar' => 'tx_myext_p1gallery[backId]', 'noMatch' => 'bypass', ], [ 'GETvar' => 'tx_myext_p1gallery[gallery]', 'lookUpTable' => [ 'table' => 'tx_myext_domain_model_gallery', 'id_field' => 'uid', 'alias_field' => 'title', 'addWhereClause' => 'AND NOT deleted', 'useUniqueCache' => 1, 'useUniqueCache_conf' => [ 'strtolower' => 1, 'spaceCharacter' => '-', ], ], ], [ 'GETvar' => 'tx_myext_p1gallery[page]', ], ], 循环中返回,因此它只会检查第一个for项。

相反,您可以执行以下操作:

config

这样,它将尝试 return value.filter(result => { for (let i = 0; i < config.length; i ++) { const type = config[i]; if (result[type].toString().toLowerCase().indexOf(q) > -1) { return true; } } return false; }); 数组中的每个值,并且如果找不到匹配项,则仅返回false。

或者,使用config函数,您可以执行以下操作:

some