【问题标题】:Angular Filter pipe not working properly角过滤管无法正常工作
【发布时间】:2018-02-20 09:14:03
【问题描述】:

我在数据库的两个不同列中有名字和姓氏,所以我必须以这种方式绑定它以在我的表中显示为 1。

但是,当我在名字和姓氏之间输入空格时,我的搜索功能不起作用:

<tbody>
    <tr *ngFor="let e of employeelist | filter : searchByName">
        <td>{{e.firstName}} {{e.lastName}}</td>


 import { Injectable, Pipe, PipeTransform } from '@angular/core';

  @Pipe({
    name: 'filter'
   })

   @Injectable()
  export class FilterEmployeesPipe implements PipeTransform {
      transform(value: any, input: string) {
    if (input) {
   input = input.toLowerCase();
   //filter je filter() metod iz array.prototype.filter
   return value.filter(function (employee: any) {

       return ((employee.firstName.toLowerCase().indexOf(input)) && 
     (employee.lastName.toLowerCase().indexOf(input))) > -1;
      })
   }
   return value;
    }
     }

【问题讨论】:

  • 你能格式化你的代码吗?
  • 如果需要,您可以查看formatting

标签: angular pipe


【解决方案1】:

像这样使用

<td>{{e.firstName + '&nbsp;' +e.lastName}}</td>

【讨论】:

  • 是的,但我的搜索仍然无法正常工作,我应该在管道中返回什么?
【解决方案2】:

您需要在用于匹配输入的字符串中包含空格

return (employee.firstName.toLowerCase() + ' ' employee.lastName.toLowerCase()).indexOf(input) > -1;

否则你就是在比较苹果和梨。

【讨论】:

    猜你喜欢
    • 2019-12-21
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    • 2015-02-14
    • 2013-08-16
    • 2012-11-16
    相关资源
    最近更新 更多