Angular:如何使用查询参数过滤dataTable

时间:2019-07-02 08:35:17

标签: css angular typescript angular-material material-design

我想使用从另一个组件的用户输入获得的查询参数过滤表。

我能够获得用户通过输入发送的数据并将其打印到console.log。但是我不知道如何使用它来过滤表格。

我已经建立了一个过滤器,但由于某种原因我无法调用它。

这是我的过滤器

import { Pipe, PipeTransform } from "@angular/core";
import { Container } from "./Entites/Container";

@Pipe({
  name: 'textFilter'
})
export class textFilter implements PipeTransform {

  transform(
    containers : Container[],
    storageSearch?: any,
    clientSearch?: string,
  ): Container[] {

    if (!containers) return [];
    if (!storageSearch) return containers;
    storageSearch = storageSearch.toLocaleLowerCase();
    containers = [...containers.filter(user => user.TAOR_QTSR_EBRI.toLocaleLowerCase() ===  storageSearch)];

    if (!clientSearch) return containers;
    clientSearch = clientSearch.toLocaleLowerCase();
    containers = [...containers.filter(user => user.LQOCH_SHM_LEOZI_QTSR.toLocaleLowerCase() ===  clientSearch)];

  // if (!roleSearch) return users;
  //roleSearch = roleSearch.toLocaleLowerCase();
  //users = [...users.filter(user => user.role.toLocaleLowerCase() ===  roleSearch)];

    return containers;
  }
}

这是我的组件ngOnInit,那里还有其他一些过滤器,例如复选框过滤器:

  ngOnInit() {
    this.marinService.getAllContainers().subscribe((result) => {
     //Data
      this.dataSource = new MatTableDataSource(result);
      //Paginator
      this.dataSource.paginator = this.paginator;
      //AutoFilter Form 1st page
      this.clientType = this.route.snapshot.queryParamMap.get('clientType');
      this.storageType= this.route.snapshot.queryParamMap.get('storageTypes');
      console.log('The Client name is : '+this.clientType+'  '+'The storage Facility is : '+this.storageType);
      //CheckBox Filter
      this.dataSource.filterPredicate = (data: Container, filter: any) => {
        return filter.split(',').every((item: any) => data.SOG_MCOLH.indexOf(item) !== -1);
      };

      this.filterCheckboxes.subscribe((newFilterValue: any[]) => {
        this.dataSource.filter = newFilterValue.join(',');
      });

    });
  }

我要完成的工作是能够使用查询参数过滤表。

1 个答案:

答案 0 :(得分:0)

我们可以将您作为输入接收的数据(在父组件中)传递给子组件内部的物料表过滤功能applyFilter ...

相关的父TS

import { Component } from '@angular/core';
@Component({
  selector: 'app-root',
  styles: [`.parent{background:lightgreen; padding:2%;}`],
  template: `
  Enter string for filtering: <input type='text' [(ngModel)]='inputStr' />
  <!-- {{inputStr}} -->
  <table-filtering-example [inputStr]='inputStr'>loading</table-filtering-example>
  `,
})
export class AppComponent {
  inputStr: string = '';
  constructor() { }
}

相关的子TS

export class TableFilteringExample implements OnInit, OnChanges {
  displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
  dataSource = new MatTableDataSource(ELEMENT_DATA);
  @Input() inputStr:string;

  constructor(){}

  ngOnInit(){}

  ngOnChanges(){
     /* just call the applyFilter button with the data which is passed to your component from it's parent */console.log("(ngOnChanges)this.inputStr:", this.inputStr); 
     this.applyFilter(this.inputStr);
     }

  applyFilter(filterValue: string) {
    this.dataSource.filter = filterValue.trim().toLowerCase();
  }
}

完成working stackblitz here