Angular 2搜索过滤器适用于一个属性,但不适用于阵列的所有属性

时间:2017-04-08 16:50:31

标签: angular ionic-filter-bar angular2-filtering

在Angular 1中,如上所述here有过滤属性和ng-repeat

但在Angular 2中,有一个名为" pipe" 的新功能。我已经使用过管道,但我仍然无法搜索Angular 1版本中的所有属性。

然而,它适用于任何1个属性,但对于整个数组,它不起作用。

当我搜索某些内容时出现以下错误。

  

错误类型错误:无法读取属性'包含'为null             在http://localhost:8100/build/main.js:118751:76             在Array.some(本机)             在http://localhost:8100/build/main.js:118751:42             at Array.filter(native)             在FilterPipe.transform(http://localhost:8100/build/main.js:118750:26)               at checkAndUpdatePureExpressionInline(http://localhost:8100/build/main.js:11519:38)               at checkAndUpdateNodeInline(http://localhost:8100/build/main.js:12374:17)               at checkAndUpdateNode(http://localhost:8100/build/main.js:12336:16)               在debugCheckAndUpdateNode(http://localhost:8100/build/main.js:12965:59)               在debugCheckDirectivesFn(http://localhost:8100/build/main.js:12906:13)               在Object.eval [as updateDirectives](ng:///AppModule/Orders.ngfactory.js:628:46)               at Object.debugUpdateDirectives [as updateDirectives](http://localhost:8100/build/main.js:12891:21)               在checkAndUpdateView(http://localhost:8100/build/main.js:12303:14)               在callViewAction(http://localhost:8100/build/main.js:12618:17)               在execComponentViewsAction(http://localhost:8100/build/main.js:12564:13

     

ERROR CONTEXT DebugContext_ {view:Object,nodeIndex:43,nodeDef:Object,elDef:Object,elView:Object}组件:   (...)componentRenderElement:(...)context:(...)elDef:   ObjectbindingFlags:2bindingIndex:6bindings:Array(1)childCount:   1childFlags:73728childMatchedQueries:0directChildFlags:   73728element:Objectflags:1index:42matchedQueries:   ObjectmatchedQueryIds:0ngContent:nullngContentIndex:0outputIndex:   3outputs:Array(0)parent:Objectprovider:nullquery:nullreferences:   ObjectrenderParent:Objecttext:null__proto __:ObjectelOrCompView:   (...)elView:Objectinjector:(...)nodeDef:ObjectnodeIndex:   43providerTokens :( ...)引用:(...)renderNode:(...)view:   Object__proto__:Object

以下是我的参考代码:

1)FilterPipe.ts

import { Pipe, PipeTransform, Injectable } from '@angular/core';
@Pipe({
    name: 'filter'
})
@Injectable()
export class FilterPipe implements PipeTransform {

    transform(items: any, term: any): any {
        if (term) {
            // THIS IS WORKING BUT FOR ONLY order_id PROPERTY
            // return items.filter(function (item) {
            //     return item.order_id.toLowerCase().includes(term.toLowerCase());
            // });

            // THIS IS NOT WORKING
            return items.filter(item => {
                return Object.keys(item).some(k => item[k].includes(term.toLowerCase()));
            });
        }
        return items;
    }
}

2)Orders.html

<ion-header style="direction: ltr;">
  <ion-navbar>
    <ion-buttons left>
      <button ion-button icon-only (click)="logout()">
        <ion-icon name="log-out"></ion-icon>
      </button>
    </ion-buttons>

    <ion-title>הזמנה</ion-title>

    <ion-buttons start>
      <button ion-button icon-only (click)="getOrders()">
        <ion-icon name="refresh"></ion-icon>
      </button>
    </ion-buttons>

    <ion-buttons end>
      <button ion-button icon-only (click)="showFilterBar()">
        <ion-icon name="search"></ion-icon>
      </button>
    </ion-buttons>
  </ion-navbar>
</ion-header>

<ion-content>
    <ul class="list" *ngFor="let order of orders | filter : Search.value">
    <li class="item {{order.color}}" (click)="gotoorderdetails(order)">
      <div style="float:left;">
        {{order.start_date}}<br/>
      </div>
      <b>{{order.order_id}} </b> &nbsp;&nbsp;&nbsp;צ - {{order.total_staff}} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ימ- {{order.number_of_days}}<br/><br/>      {{order.full_name}}
      <br/>
      <div style="float:left;">
        {{order.managers}}<br/>
      </div>
      <span *ngIf="order.event_location"> {{order.event_location}}<br/></span>
    </li>
  </ul>
</ion-content>
<ion-footer>
  <ion-searchbar #Search (keyup)="0"></ion-searchbar>
</ion-footer>

如何在离子2中使用ionic filter bar

1 个答案:

答案 0 :(得分:2)

感谢JB Nizet的回答。

以下是我工作的更新代码。

import { Pipe, PipeTransform, Injectable } from '@angular/core';
@Pipe({
    name: 'filter'
})
@Injectable()
export class FilterPipe implements PipeTransform {

    transform(items: any, term: any): any {
        if (term) {
            return items.filter(item => {
                return Object.keys(item).some(
                    k => {
                        if (item[k] != null && item[k] != undefined && typeof item[k] == 'string')
                            return item[k].includes(term.toLowerCase());
                    }
                );
            });
        }
        return items;
    }
}