【发布时间】:2020-01-21 06:46:42
【问题描述】:
Below is my table view. Each column is having search
根据我在特定列中键入的文本,仅需要搜索该列。
我面临的问题是: 当我输入一列时,其他列中也会出现相同的文本。为了解决这个问题,我使用了以下代码:
<tr>
<th *ngFor="let head of headElements; let i = index" scope="col">
<h5 class="text-color">{{head}}</h5>
<div class="md-form mb-0">
<input [(ngModel)]="searchText[i]" type="text" class="form-control"
id="search_{{i}}" mdbInput />
<label for="search_{{i}}">Search</label>
</div>
</th>
</tr>
通过给[(ngModel)]="searchText[i]" 或[(ngModel)]="searchText[head]" 我可以在不同的输入框中输入不同的文本。
我无法获取在过滤器中输入的文本:searchText;
下面是我查看表格数据的代码
<tbody>
<tr *ngFor="let el of elements | filter : searchText; let i = index" mdbWavesEffect>
<td class="p-2" *ngIf="i+1 >= mdbTablePagination.firstItemIndex && i < mdbTablePagination.lastItemIndex"
scope="row">{{el.firstName}} {{el.middleName}} {{el.lastName}}</td>
<td class="p-2" *ngIf="i+1 >= mdbTablePagination.firstItemIndex && i < mdbTablePagination.lastItemIndex">
{{el.mobileNumber}}</td>
<td class="p-2" *ngIf="i+1 >= mdbTablePagination.firstItemIndex && i < mdbTablePagination.lastItemIndex">
{{el.joiningYear}}</td>
<td class="p-2" *ngIf="i+1 >= mdbTablePagination.firstItemIndex && i < mdbTablePagination.lastItemIndex">
{{el.emailId}}</td>
<td class="p-2" *ngIf="i+1 >= mdbTablePagination.firstItemIndex && i < mdbTablePagination.lastItemIndex">
{{el.designation}}</td>
</tr>
</tbody>
接下来我在 conponent.ts 文件中声明了searchText: any = [];,否则它会给出未定义的错误
最后是我的过滤管道代码
if (!employees || !searchTerm) {
return employees;
}
return employees.filter(employee =>
employee.mobileNumber.toLowerCase().indexOf(searchTerm) !== -1);
}
【问题讨论】:
-
嗨@Talha,你能把这个放在stackblitz中吗?这样很容易检查问题。
-
@SatishPai 我正在使用本地休息服务获取员工数据。所以我无法完全配置项目。这是链接stackblitz.com/edit/angular-pl8mua
标签: angular typescript filter pipe angular7