【问题标题】:Column based search using angular filter pipe使用角度过滤管的基于列的搜索
【发布时间】: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


【解决方案1】:

不要在&lt;tr&gt; 元素上使用过滤器管道,而是尝试在&lt;input&gt; 元素上监听更改事件。

<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]" (change)="filterElements(searchText[i])" type="text" class="form-control" 
                  id="search_{{i}}" mdbInput />
                <label for="search_{{i}}">Search</label>
            </div>
        </th>
    </tr>

在组件类中:

private elementsCopy = Object.assign({}, this.elements);
public filterElements(searchTerm){
    if (!this.elementsCopy || !searchTerm) {
        return this.elementsCopy ;
      }
  return this.elementsCopy.filter(employee =>
    employee.mobileNumber.toLowerCase().indexOf(searchTerm) !== -1);

}

希望对你有帮助。

【讨论】:

  • 我认为这会起作用,因为我们将发送有关更改的数据。我会尝试并告诉你。但我需要使用管道。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多