使用ngx-filter-pipe在Angular中过滤多个值

10

我正在使用Angular 4的ngx-filter-pipe,并且遇到了问题。我已经成功地使用一个值进行筛选,现在我尝试使用多个值来筛选数据:这是我的组件:

@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);
      }
    );
  }
}
这是我的观点
<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>

我试了很多方法,但是无法使它生效。这是我正在遵循的文档 https://github.com/VadimDez/ngx-filter-pipe 我无法像显示的那样使用$ or 过滤器 enter image description here

这是我从ws获取的数据

enter image description here

正如你所看到的,我正在使用mes来进行筛选,但我想使用更多的筛选条件: <input type="text" name="term" [(ngModel)]="userFilter.mes" class="form-control" id="term" placeholder="Buscar por mes...">

非常感谢您的帮助。

3个回答

3
如果您使用相同的数据类型来过滤一个值,您可以在同一个过滤器中完成。但是,如果您想要不同的结果,您可以根据您的要求使用if else条件或switch条件来获得不同的结果。

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;
    }
}


谢谢您的回答,但我已经有了筛选器,我只需要一些帮助让它适用于多个值。我会编辑我的帖子以提供更多细节。 - Rildo Gomez

2

不确定我是否正确理解了您的问题。

您想按多个字段进行过滤吗?如果是这样-只需将更多字段设置为过滤对象

例如,如果您的过滤对象是userFilter,并且您已经有一个输入将值设置为字段mes

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

添加另一个输入,例如将值设置为按Annio筛选:

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

希望这可以帮助您


是的,那就是我想做的,不过我在想是否可以在一个输入中使用它。 - Rildo Gomez
你想要筛选哪些字段呢? - vadimdez
假设我想过滤“mes”,“Annio”和“numero_dpto”。这可行吗? - Rildo Gomez
我的意思是使用一个输入,让管道过滤这三个字段。发送类似于public userFilter: any = { mes: '', Annio: '', numero_dpto:'' };的内容。 我尝试使用$or来实现,但不确定出了什么问题,或者是否可能实现。 - Rildo Gomez
我想按照开头而不是包含来搜索,有人知道我如何在ngx-filter-pipe中实现吗? - Ayush Somani

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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接