本文介绍了使用Angular中的一个管道过滤表中的多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

component.html

component.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>

pipe.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()) );
}

推荐答案

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

Created multiple arguments pipe in angular 4

  1. 值:其中涉及表内的所有数据,所有列
  2. searchString:要在列内(表内)搜索的内容.

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

Hence, you can define which columns to be searched inside the transform function.

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

In this case, the columns to be searched are builderId, groupName and 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 );      
       }) 
    }
}
  1. builderId,groupName和companyPersonName是我搜索的三个字段

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

builderId转换为字符串,因为我们的searchString是字符串格式.

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

toLowerCase()用于使搜索准确无视用户搜索是小写还是大写

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>

这篇关于使用Angular中的一个管道过滤表中的多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 17:19