filtering more than one value with ngx-filter-pipe angular
Asked Answered
M

3

10

I'm using ngx-filter-pipe with angular 4 and I'm stuck with this issue. I managed to filter with one value and now im trying to filter data with more than one value: Here's what I got This is my component:

@Component({
  selector: 'deudas-list',
  templateUrl: '../views/deudas-list.html',
  providers: [ DeudaService ]
})
export class DeudasListComponent{
  public titulo: string;
  public  deudas: Deuda[];
  public  userFilter: any = { mes: '' };

  constructor(
      private _route: ActivatedRoute,
      private _router: Router,
      private _productoService: DeudaService
  ) {
    this.titulo = 'Listado de Pagos:';
  }

  ngOnInit() {
    this._productoService.getDeudas().subscribe(
      result =>{
          console.log(result['Cuotas'].Cuota);
          this.deudas = result['Cuotas'].Cuota;
      },
      error => {
        console.log(<any>error);
      }
    );
  }
}

This is my view

<div class="form-group has-feedback">
  <label for="term" class="sr-only">Search</label>
  <input type="text" name="term" [(ngModel)]="userFilter.mes" class="form-control" id="term" placeholder="Buscar por mes...">
  <span class="glyphicon glyphicon-search form-control-feedback"></span>
</div>
<div *ngIf="deudas" class="table-responsive col-lg-12 tablilla">
  <table class="table table-bordered table-striped table-responsive">
    <tbody>
      <tr *ngFor="let deuda of deudas | filterBy: userFilter | paginate: {itemsPerPage:10,currentPage: p}">
        <td>{{deuda.nombre_edificio}}</td>
        <td>{{deuda.numero_dpto}}</td>
        <td>{{deuda.Annio}}</td>
        <td>{{deuda.mes}}</td>
        <td>{{deuda.nombre_tipo_cuota}}</td>
        <td>{{deuda.monto_cuota}}</td>
        <td>{{deuda.nombre_estado_cuota}}</td>
        <td>{{deuda.fecha_pago}}</td>
      </tr>
    </tbody>
  </table>
  <pagination-controls (pageChange)="p = $event" class="paginador"></pagination-controls>
</div>

I tried multiple things but I cant make it work Here's the documentation I'm following https://github.com/VadimDez/ngx-filter-pipe I cant get $or filter to work as shown enter image description here

This is the data I'm getting from a ws

enter image description here

as you can see im using mes to filter but i'd like to use more than one <input type="text" name="term" [(ngModel)]="userFilter.mes" class="form-control" id="term" placeholder="Buscar por mes...">

any help would be appreciated.

Mayemayeda answered 18/9, 2017 at 23:12 Comment(0)
C
3

if you are using same data type for filtering a value you can do it in same filter but if you want different result you can use if else conditions or switch condition for different result as your requirements

import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
    name: 'CurrencyFilter',
    pure: false
})
export class CurrencyFilterPipe implements PipeTransform {
    transform(items: number): any {
        var OrigionalValue = items;
        var franctionCount = parseInt(abp.session.crNoOfDgtsAftrDecimal);
        var value = items.toFixed(franctionCount);
        var commaSaperatedVal = value.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1" + abp.session.crDgtGrpSymbol + "");
       var selectedSymbol = abp.session.crCurrencySymbol;
        var selectedSymbol = "";
      var PosCurrFormat = abp.session.crPositivCurrencyFrmt;
        var NgtvCurrFormat = abp.session.crNagativeCurrencyFrmt;
        var FinalValue;
        if (items > 0)
        {
            switch (PosCurrFormat) {
                case "$1.1":
                    FinalValue = selectedSymbol + commaSaperatedVal;
                    break;
                case "1.1$":
                    FinalValue = commaSaperatedVal + selectedSymbol ;
                    break;
                case "$ 1.1":
                    FinalValue = selectedSymbol+" "+ commaSaperatedVal;
                    break;
                case "1.1 $":
                    FinalValue = commaSaperatedVal + " " + selectedSymbol;
                    break;
            }
            return FinalValue
        }
        if (items < 0) {
             commaSaperatedVal = commaSaperatedVal.replace('-','')
            switch (NgtvCurrFormat) {
                case "($1.1)": FinalValue = "("+selectedSymbol + commaSaperatedVal+")"; break;
                case "-$1.1": FinalValue = "-"+selectedSymbol + commaSaperatedVal; break;
                case "$-1.1": FinalValue = selectedSymbol + "-"+ commaSaperatedVal; break;
                case "$1.1-": FinalValue = selectedSymbol + commaSaperatedVal + "-" ; break;
                case "(1.1$)": FinalValue = "(" + commaSaperatedVal + selectedSymbol + ")"; break;
                case "-1.1$": FinalValue = "-" + commaSaperatedVal + selectedSymbol; break;
                case "1.1-$": FinalValue = commaSaperatedVal + "-"+ selectedSymbol ; break;
                case "1.1$-": FinalValue = commaSaperatedVal+selectedSymbol + "-"; break;
                case "-1.1 $": FinalValue = "-" + commaSaperatedVal +" "+ selectedSymbol ; break;
                case "-$ 1.1": FinalValue = "-" + " "+ selectedSymbol + commaSaperatedVal; break;
                case "1.1 $-": FinalValue = commaSaperatedVal + " " + selectedSymbol  + "-"; break;
                case "$ 1.1-": FinalValue = selectedSymbol + " " + commaSaperatedVal + "-"; break;
                case "$ -1.1": FinalValue = selectedSymbol + " " + "-" + commaSaperatedVal; break;
                case "1.1- $": FinalValue = commaSaperatedVal + "-" + " " + selectedSymbol; break;
                case "($ 1.1)": FinalValue = "(" + selectedSymbol + " " + commaSaperatedVal + ")"; break;
                case "(1.1 $)": FinalValue = "(" + commaSaperatedVal +" "+ selectedSymbol + ")"; break;
            }
            return FinalValue
        }
        return FinalValue;
    }
}
Cyst answered 21/9, 2017 at 5:9 Comment(1)
thank you for answering but I already have the filter I just need some help making it work for more than one value. ill edit my post for more details.Mayemayeda
H
2

Not sure if i got your question correctly.

Do you want to filter by more than one field ? If so - just set more fields to filter object

for example if your filter object is userFilter and your already have one input that sets value to field mes

<input type="text" name="term" [(ngModel)]="userFilter.mes" class="form-control" id="term" placeholder="Buscar por mes...">

add another input that sets value for example to filter by Annio:

<input type="text" name="Annio" [(ngModel)]="userFilter.Annio" class="form-control" placeholder="Annio">

Hope this helps

Hyperphagia answered 22/9, 2017 at 20:38 Comment(5)
yes, that's what I'm trying to do however I wonder if I could use it in one inputMayemayeda
What fields do you want to filter then ?Hyperphagia
let's say I want to filter "mes", "Annio" and "numero_dpto". could it be done?Mayemayeda
I mean using one input have the pipe filter those 3 fields. send something like public userFilter: any = { mes: '', Annio: '', numero_dpto:'' }; I was trying to do it with $or but I'm not sure what went wrong or if it is even possible.Mayemayeda
I want to search not by contains but starting with, Do any one know how i can do that with ngx-filter-pipe?Chorus
O
1
    @Component({
      selector: 'deudas-list',
      templateUrl: '../views/deudas-list.html',
      providers: [ DeudaService ]
    })
    export class DeudasListComponent{
      public titulo: string;
      public  deudas: Deuda[];
      public textFilter:string = ""
      public  userFilter: any = { mes: '' , Annio : '' };

      constructor(
          private _route: ActivatedRoute,
          private _router: Router,
          private _productoService: DeudaService
      ) {
        this.titulo = 'Listado de Pagos:';
      }

       applyNGXFilter(){
           this.userFilter = {
               $or : [ 
                   { 'mes' : this.textFilter} 
               ] , 
               [ 
                   { 'Annio' : this.textFilter } 
               ]
           }
      }

      ngOnInit() {
        this._productoService.getDeudas().subscribe(
          result =>{
              console.log(result['Cuotas'].Cuota);
              this.deudas = result['Cuotas'].Cuota;
          },
          error => {
            console.log(<any>error);
          }
        );
      }
    }

<div class="form-group has-feedback">
  <label for="term" class="sr-only">Search</label>
  <input type="text" name="term" [(ngModel)]="textFilter" (change)="applyNGXFilter()" class="form-control" id="term" placeholder="Buscar por mes...">
  <span class="glyphicon glyphicon-search form-control-feedback"></span>
</div>
<div *ngIf="deudas" class="table-responsive col-lg-12 tablilla">
  <table class="table table-bordered table-striped table-responsive">
    <tbody>
      <tr *ngFor="let deuda of deudas | filterBy: userFilter | paginate: {itemsPerPage:10,currentPage: p}">
        <td>{{deuda.nombre_edificio}}</td>
        <td>{{deuda.numero_dpto}}</td>
        <td>{{deuda.Annio}}</td>
        <td>{{deuda.mes}}</td>
        <td>{{deuda.nombre_tipo_cuota}}</td>
        <td>{{deuda.monto_cuota}}</td>
        <td>{{deuda.nombre_estado_cuota}}</td>
        <td>{{deuda.fecha_pago}}</td>
      </tr>
    </tbody>
  </table>
  <pagination-controls (pageChange)="p = $event" class="paginador"></pagination-controls>
</div>
Overtake answered 9/4, 2019 at 12:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.