Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 自定义管道更多参数_Angular - Fatal编程技术网

Angular 自定义管道更多参数

Angular 自定义管道更多参数,angular,Angular,我正在尝试做自定义管道,它将过滤表中的数据 我是这样做的: import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'filter' }) export class FilterPipe implements PipeTransform { transform(employees: any, term: any): any { // check if search term is undefined

我正在尝试做自定义管道,它将过滤表中的数据

我是这样做的:

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

@Pipe({
  name: 'filter'
})
export class FilterPipe implements PipeTransform {

  transform(employees: any, term: any): any {
    // check if search term is undefined
    if (term === undefined) return employees;
    // return updated array
    return employees.filter(function(alias){
      return alias.name.includes(term);
    });
  }

}
它只适用于一个参数如何用一个输入过滤任何表列(现在它只过滤名称列)

这是我的html

<div class="container-fluid">
    <div class="col-4">
        <!-- filter -->
        <form id="filter">
            <label>Search:</label>
            <input type="text" [(ngModel)]="term" name="filter">
        </form>
        <!-- end -->
        <br>
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Dept</th>
                </tr>
            </thead>
            <tbody>
                <tr *ngFor="let alias of employees | filter:term">
                    <td>{{ alias.name }}</td>
                    <td>{{ alias.dept }}</td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

搜索:

名称 部 {{alias.name} {{alias.dept}
您可以使用以下方法搜索
alias
中的所有属性:

或者,也可以使用箭头函数
=>
和:

或者更好地使用
Object.values()
(但目前没有那么好,所以您需要使用它):


但是@JBNizet的评论很贴切——你可能不想在管道中这样做。最好将该逻辑移到组件类中。

返回alias.name.includes(term)| alias.dept.includes(term)?但请阅读
return employees.filter(function(alias) {
  let found = false;
  Object.keys(alias).forEach(function(key) { 
      found = found || alias[key].includes(term);
  });
  return found;
});
return employees.filter(alias => 
    Object.keys(alias).reduce(
        (found, key) => found || alias[key].includes(term), false
    )
);
return employees.filter(alias =>
    Object.values(alias).reduce(
        (found, value) => found || value.includes(term), false
    )
);