我想为每列添加搜索框,以便在相应的列中进行搜索。为此,我编写了下面的代码,但是没有用。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>
为了使搜索框适用于每个单独的列,我需要进行哪些更改