angular - 在 Angular 4 中使用过滤器管道

标签 angular filter angular2-services angular-pipe

我在我的 Angular 4 应用程序中使用了一个过滤器管道。但是我有一个问题,因为我在一个数组内部进行过滤,但也在该数组内部进行过滤。所以我找不到那个特定的名字...因为我只能找到第一个数组而不是内部数组。下面是我的 JSON 响应和我的管道代码。在这里我只能搜索 "admin name and email" 但我不能搜索 "Outlet name, address AND Role name"

JSON

{
  "token": "eyefqffiwqnrfoqif",
  "users": [
    {
      "id": 1,
      "name": "Admin",
      "email": "admin@yahoo.com",
      "created_at": "2018-01-05 07:17:41",
      "updated_at": "2018-01-05 09:17:41",
      "outlet": {
      "id": 1,
      "name": "Sarawak Energy Berhad",
      "address": "Kuching City",
      "contact_number": "1300-88-3111",
      "created_at": "2018-01-05 10:17:44",
      "updated_at": "2018-01-05 10:17:44"
    },
      "roles": [
        {
          "id": 1,
          "name": "Admin",
          "created_at": "2018-01-05 10:17:40",
          "updated_at": "2018-01-05 10:17:40",
          "pivot": {
            "model_id": 1,
            "role_id": 1
          },
        }
      ]
    }
  ]
}

FILTER PIPE

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

@Pipe({
  name: 'search'
})
export class SearchPipe implements PipeTransform {
  transform(items: any, term: any): any {
    if (term === undefined) return items;

    return items.filter(function(item) {
      for(let property in item){
        if (item[property] === null){
          continue;
        }
        if(item[property].toString().toLowerCase().includes(term.toLowerCase())){
          return true;
        }
      }
      return false;
    });
  }
}

最佳答案

在这种情况下,您的导出是每个数组对象中的另一个对象,因此您需要专门为导出添加一个过滤器,如下所示,

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

@Pipe({
  name: 'filter'
})
export class FilterPipe implements PipeTransform {
 transform(items: any, term: any): any {
    if (term === undefined) return items;

    return items.filter(function(item) {
      for(let property in item){

        if (item[property] === null){
          continue;
        }
        if(item[property].toString().toLowerCase().includes(term.toLowerCase())){
          return true;
        }
         if(property ='outlet'){
           if(item[property].name.toString().toLowerCase().includes(term.toLowerCase())){
           console.log(item[property].name);
          return true;
         }

        }
         if(property ='roles'){
         if (item[property].filter(e => e.name === term.toLowerCase()).length > 0) {
          return true;
         }

        }
       }
      return false;
    });
  }

}

DEMO STACKBLITZ

关于angular - 在 Angular 4 中使用过滤器管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48134013/

相关文章:

angular - 如何阻止 Angular 路由器事件在控制台上打印

angular - 如何使用 Apollo 后端在 TypeScript Angular 应用程序中键入部分类型?

javascript - 使用另一个对象数组过滤对象数组

angular - 关于angular2提供者的一些问题

typescript - Angular 2 将复杂的服务注入(inject)到服务中

javascript - 在 Angular 6 项目中,@brief 是什么意思?

javascript - Typescript 组件中的“this”与 Angular2 中的 ES5

grails - 如何在 Grails 中取消过滤?

ios - 如何过滤 dic-filter 数组并附加到单独的 dic 数组

javascript - Angular2 location.back() 页面重新加载?