Angular: How to filter a dataTable using query params

Viewed 1692

I want to filter my table using query params that I got from the user input in another component.

I am able to get the data that the users send through the input and print it to the console.log. But I don't know how to use it to filter the table.

i have built a filter but for some reason i cant call it.

This is my filter :

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

This is my component ngOnInit i have some other filters there, for example checkbox filter :

  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(',');
      });

    });
  }

What I want to accomplish is to be able to filter the table using the query params.

1 Answers

We can pass the data which you have received as input (in the parent component) to the material table filtering function applyFilter inside the child component...

relevant parent 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() { }
}

relevant child 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();
  }
}

complete working stackblitz here

Related