做了一些更改:
-
在问题中的共享代码中,使用了名为
grdFilter
-
实施
getSiteName
已添加,它接受该对象并返回博客名称(我们的数据中没有名为“site”的属性)
-
在你的共享代码中
<ng-container *ngFor="let el of customerData | gridFilter: {name: searchText, site:searchText}">
-
由于我们的数据中没有名为“site”的属性,我们将其更改为
<ng-container *ngFor="let el of customerData | grdFilter: {name: searchText, blog:searchText}">
你好,康普内特
import { Component, Input } from '@angular/core';
@Component({
selector: 'hello',
template: `<input [(ngModel)]="searchText" placeholder="Search.." class="advancedSearchTextbox">
<p></p>
<table *ngFor="let emp of customerData | grdFilter: {name: searchText, Age:searchText, blog: searchText}; let i=index;">
<tr>
<td style="width: 5%;">{{i +1}}</td>
<td style="width: 10%;">{{emp.name}}</td>
<td style="width: 5%;">{{emp.Age}}</td>
<td style="width: 15%;">{{emp.blog}}</td>
</tr>
</table>
<hr/>
<table class="table table--bordered table--hover approvals-table" id="location-table">
<thead>
<tr>
<th class="sortable">{{ element | titlecase }} Name <span class="sort-indicator icon-chevron-down"></span></th>
<th class="sortable">Site <span class="sort-indicator icon-chevron-down"></span></th>
<th>Action</th>
</tr>
</thead>
<tbody>
<ng-container *ngFor="let el of customerData | grdFilter: {name: searchText, blog:searchText}">
<tr>
<td>{{el.name}}</td>
<td>{{ getSiteName(el) }}</td>
<td>
<a><span class="icon-trash" (click)="deleteElement(el.id, el.name)"></span></a>
</td><td>
<a><span class="icon-pencil" (click)="editElement(el)"></span></a>
</td>
</tr>
</ng-container>
</tbody>
</table>
`,
styles: [`h1 { font-family: Lato; }`]
})
export class HelloComponent {
public searchText: string;
public customerData: any;
@Input() name: string;
constructor() { }
ngOnInit() {
this.customerData = [
{ "name": 'Anil kumar', "Age": 34, "blog": 'https://code-view.com' },
{ "name": 'Sunil Kumar Singh', "Age": 28, "blog": 'https://code-sample.xyz' },
{ "name": 'Sushil Singh', "Age": 24, "blog": 'https://code-sample.com' },
{ "name": 'Aradhya Singh', "Age": 5, "blog": 'https://code-sample.com' },
{ "name": 'Reena Singh', "Age": 28, "blog": 'https://code-sample.com' },
{ "name": 'Alok Singh', "Age": 35, "blog": 'https://code-sample.com' },
{ "name": 'Dilip Singh', "Age": 34, "blog": 'https://code-sample.com' }];
}
getSiteName(passedObj) {
return passedObj.blog;
}
}
更新#1
如果转到grd-pipe.ts文件,并执行
console.log(items,filter)
,您将看到我们在自定义管道中处理的数组是locations数组,它的“sites”列值为1、2或3。从sites数组中为UI获取的站点名称不是locations数组的一部分,因此管道无法处理该名称。
因此,我们在locations数组中创建一个新的字段site name,在其中存储站点名称(来自sites数组),现在由于该字段在管道中可用,管道也可以对其进行搜索。
TS公司
ngOnInit(){
for(var i=0;i<this.locations.length;i++){
this.locations[i].siteName = this.sites.find(s => s.id === this.locations[i].site).name;
}
}
相关变更
添加到筛选器的字段siteName:
<table class="table table--bordered table--hover approvals-table" id="location-table">
<tbody>
<ng-container *ngFor="let loc of locations | grdFilter: {name: searchText, siteName:searchText}; let i=index">
<tr>
<td style="width: 5%;">{{i +1}}</td>
<td style="width: 10%;">{{loc.name}}</td>
<td style="width: 5%;">{{getSiteName(loc.site)}}</td>
</tr>
</ng-container>
</tbody>
</table>
工作
stackblitz here
也会更新