要为将在相应列中搜索的每一列添加搜索框

问题描述 投票:1回答:1

enter image description here我想为每列添加搜索框,以便在相应的列中进行搜索。为此,我编写了下面的代码,但是没有用。home.component.html

<table class="table">
  <tr>
    <th>Commit Date</th>
    <th>Commit Id</th>
    <th >Environment</th>
    <th>Jira Id's</th>
    <th >Upgrade Date</th>
  </tr>
  <tr>
    <th><input [(ngModel)]="searchString1" placeholder="Search.." class="advancedSearchTextbox"></th>
    <th><input [(ngModel)]="searchString2" placeholder="Search.." class="advancedSearchTextbox"></th>
    <th><input [(ngModel)]="searchStrin3" placeholder="Search.." class="advancedSearchTextbox"></th>
    <th><input [(ngModel)]="searchStrin4" placeholder="Search.." class="advancedSearchTextbox"></th>
    <th><input [(ngModel)]="searchString5" placeholder="Search.." class="advancedSearchTextbox"></th>
  </tr>
  <tr *ngFor="let data of history | filter :{ commit_date : searchString1, commit_id: searchString2, env: searchString3, jira_ids: searchString4, upgrade_date: searchString5};">
    <td class="text-left">
      {{data.commit_date}}
    </td>
    <td>{{data.commit_id}}</td>
    <td>{{data.env}}</td>
    <td>{{data.jira_ids}}</td>
    <td>{{data.upgrade_date}}</td>
  </tr>
</table>

filter.pipe.ts

import { Pipe, PipeTransform, Injectable } from '@angular/core';
@Pipe({
  name: 'filter'
})
@Injectable()
export class FilterPipe implements PipeTransform {
  transform(items: any, filter: any, defaultFilter: boolean): any {
    if (!filter){
      return items;
    }
    if (!Array.isArray(items)){
      return items;
    }
    if (filter && Array.isArray(items)) {
      let filterKeys = Object.keys(filter);
      if (defaultFilter) {
        return items.filter(item =>
            filterKeys.reduce((x, keyName) =>
                (x && new RegExp(filter[keyName], 'gi').test(item[keyName])) || filter[keyName] == "", true));
      }
      else {
        return items.filter(item => {
          return filterKeys.some((keyName) => {
            return new RegExp(filter[keyName], 'gi').test(item[keyName]) || filter[keyName] == "";
          });
        });
      }
    }
  }
}

如果我在顶部添加一个搜索框并按如下所示更改HTML文件,则效果很好:

Search Data Here: </b><input [(ngModel)]="searchString" placeholder="Search.." class="advancedSearchTextbox">
<table class="table">
  <tr>
    <th>Commit Date</th>
    <th>Commit Id</th>
    <th >Environment</th>
    <th>Jira Id's</th>
    <th >Upgrade Date</th>
  </tr>
  <tr *ngFor="let data of history | filter :{ commit_date : searchString, commit_id: searchString, env: searchString, jira_ids: searchString, upgrade_date: searchString};">
    <td class="text-left">
      {{data.commit_date}}
    </td>
    <td>{{data.commit_id}}</td>
    <td>{{data.env}}</td>
    <td>{{data.jira_ids}}</td>
    <td>{{data.upgrade_date}}</td>
  </tr>
</table>

为了使搜索框适用于每个单独的列,我需要进行哪些更改

html angular7
1个回答
0
投票
使用一个参数作为搜索输入和使用功能,如下所示获取键和列名:
© www.soinside.com 2019 - 2024. All rights reserved.