How to make a pipe filter in Ionic 2?

0

In the version of Angular 2 they removed the pipe Filter that filtered any coincidence in any property of a list of objects, in the documentation it says that if you need a pipe Filter that we manufacture it, but I do not know how to do this.

What I need is:

 <ion-searchbar [(ngModel)]="buscar"></ion-searchbar>
 <div *ngFor="let producto of productos | Filter: buscar ">
     <div> {{producto.nombre}} </div>
     ...
 </div>
    
asked by Daniel Enrique Rodriguez Caste 27.06.2017 в 20:09
source

1 answer

1

Daniel, good, I'll give you an example that I've been using for simple filter tables:

transform(values: [], value: string): string { 
    let ret = [];
    let columns = Object.getOwnPropertyNames(values[0]);
    let encontrado = false;


    if(value == '' || value == undefined){
      return values;
    }

    for(var i in values){
      encontrado = false;
      for(var x in columns){
        if(values[i][columns[x]].toUpperCase().indexOf(value.toUpperCase()) > -1 && !encontrado){
          encontrado = true;
          ret.push(values[i]);
        }
      }
    }

  return ret; 
  } 

Functional example in plnkr: link

A slightly more complex example that allows more complex operations: link

I hope you find it useful, and any questions, ask everything you need.

    
answered by 28.06.2017 / 23:11
source