javascript - 使用 Angular 中的一个管道过滤表中的多列

标签 javascript angular typescript

Hi everyone. I want to make a custom filter for my table which intakes more than one argument to search multiple columns .. in my case right now only one argument can be passed . thanks in advance

组件.html

<tr *ngFor = "let builder of builderDetailsArray[0] | filter :'groupName': searchString; let i = index" >
    <td style="text-align: center;"><mat-checkbox></mat-checkbox></td>
    <td>{{builder.builderId}}</td>
    <td>{{builder.viewDateAdded}}</td>
    <td>{{builder.viewLastEdit}}</td>
    <td>{{builder.groupName}}</td>
    <td>{{builder.companyPersonName}}</td>
    <td style="text-align: center;"><button mat-button color="primary">UPDATE</button></td>
</tr>

管道.ts

@Pipe({
    name: "filter",
    pure:false
})

export class FilterPipe implements PipeTransform {
    transform(items: any[], field: string, value: string): any[] {
    if (!items) {
        return [];
    }
    if (!field || !value) {
        return items;
    }
    return items.filter(singleItem => 
        singleItem[field].toLowerCase().includes(value.toLowerCase()) );
}

最佳答案

在 Angular 4 中创建了多个参数管道

The code lets you search through multiple columns in your table.

Passed 2 arguments in the transform function

  1. value: Which involves all the data inside the table, all columns
  2. searchString: What you want to search inside the columns (inside the table).

因此,您可以在转换函数中定义要搜索的列。

在这种情况下,要搜索的列是 builderId、groupName 和 companyPersonName

管道文件

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

@Pipe({
    name: "arrayFilter"
})

export class BuilderFilterPipe implements PipeTransform {

    transform(value:any[],searchString:string ){

       if(!searchString){
         console.log('no search')
         return value  
       }

       return value.filter(it=>{   
           const builderId = it.builderId.toString().includes(searchString) 
           const groupName = it.groupName.toLowerCase().includes(searchString.toLowerCase())
           const companyPersonName = it.companyPersonName.toLowerCase().includes(searchString.toLowerCase())
           console.log( builderId + groupName + companyPersonName);
           return (builderId + groupName + companyPersonName );      
       }) 
    }
}

What does the transform function do?

  1. builderId, groupName and companyPersonName are the three fields I searched

  2. builderId converted to string because our searchString is in string format.

  3. toLowerCase() is used to make search accurate irrespective of user search in lowercase or uppercase

HTML:

  <tr *ngFor = "let builder of newBuilderDetailsArray | arrayFilter:search" >
      <td>{{builder.builderId}}</td>
      <td>{{builder.groupName}}</td>
      <td>{{builder.companyPersonName}}</td> 
  </tr>

enter image description here

enter image description here

Make sure your filter.ts file added to module.ts file

关于javascript - 使用 Angular 中的一个管道过滤表中的多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47631908/

相关文章:

javascript - 在 Angular 6 的 do while 循环中延迟递归 HTTP get 请求

javascript - 失败的 Prop 类型。我的 redux 存储中未定义所需的 prop 类型

angular - angular2 是否可以从另一个 angular2 项目导入组件?

html - 格式化数据 block

linux - 从 TypeScript 运行任何 Linux 终端命令?

node.js - 如何使用 Typescript Express 应用程序渲染 Lit Element Web 组件?

javascript - Asp.net MVC模态弹出窗口确认后关闭

javascript - IE10 和图像/ Canvas 的跨域资源共享 (CORS) 问题

javascript - 是否有一种干净的方法来访问命名空间内的私有(private)变量

php - 如何创建录音WebApp?