angular - 使用现有过滤器以 Angular 过滤多列

标签 angular html-table pipe

所以我正在使用过滤器,我的问题是我让过滤器在单个列上工作,我如何使用这个现有的过滤器来搜索多个列。我是 Angular 新手,无法集中注意力这任何指导都会很有帮助

HTML

<input type="text" placeholder="Enter INC to search" class="float-right mt-4 mr-4 form-control w-20" [(ngModel)]="searchQuery" (ngModelChange)="searchtable('issues', issues)">

<table class="table" id="someID">
                                    <thead>
                                        <tr>
                                            <th>id</th>
                                            <th>name</th>
                                            <th>Domain</th>
                                            <th>age</th>
                                            <th>date</th>
                                            
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr *ngFor="let x of issues | FilterPipe: ticketNumber ">
      
                                            <td>{{ x.id}}</td>
                                            <td>{{ x.name }}</td>
                                            <td>{{ x.age }}</td>
                                            <td>{{ x.type }}</td>
                                            <td>{{ x.phone }}</td>
                                            <td>{{ x.date }}</td>

                                        </tr>
                                    </tbody>
                                </table>

ts 文件

 public searchtable(type, data) {
    if (this.searchQuery === "") {
      this.isSearching = false;
      if (type === "requests") {
        this.requests = this.allRequests;
      } else if (type === "issues") {
        this.issues = this.allIssues;
      }
    } else {
      this.isSearching = true;
      if (type === "requests") {
        this.requests = this.allRequests.filter(res => {
          return res.id.toLocaleLowerCase().match(this.searchQuery.toLocaleLowerCase());
        });
      } else if (type === "issues") {
        this.issues = this.allIssues.filter(res => {
          return res.id.toLocaleLowerCase().match(this.searchQuery.toLocaleLowerCase());
        });
      }
    }
  }

pipe.ts

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

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

  transform(value: any, input: any): any {
   if (input) {
     return value.filter(val => val.toLowerCase().indexOf(input.toLowerCase()) >= 0);
   } else {
     return value;
   }
  }

}

最佳答案

您还可以将对象传递到管道。像这样的事情:

<div *ngFor="let x of issues | FilterPipe: {ticketNumber, someKey: someValue}">
  {{x | json}}
</div>
管道中的

input 将包含这些值,您可以使用它们。

旁注:我建议根据上下文命名变量。例如。 问题的问题而不是x的问题TicketFilterPipe而不是FilterPipe,等等...

关于angular - 使用现有过滤器以 Angular 过滤多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68073049/

相关文章:

javascript - 'as' 语句 Angular 2 +/javascript

javascript - 表格单元格自动设置valign

linux - 如何将 Linux which 命令的输出通过管道传输到 Linux 文件命令中?

python - 防止第二个进程监听 Python 中的同一个管道

javascript - 收到 fcm 推送通知后更新页面上的 View 并且我在同一页面上

node.js - Angular 2在不重新启动的情况下不显示图像

java - 如何计算总数使用 selenium webdriver 从特定 TR 标签中提取 TD 标签

html - 表格内的单选按钮

通过管道重定向时捕获 SetConsoleTextAttribute 的效果?

使用注入(inject)提供程序的 Angular 异步表单验证