title: Filtering by column in custom Angular data table
body: <p>I am making a reusable Angular data table component where you have the ability to filter by column. Right now, my approach works but I am concerned that this in not the best practice in Angular. Is there another approach to filtering individual columns other than adding that specific column to "selectColInput" when clicking in the input, and then using that in a filter pipe? </p>
  
  <p>data-table.component.html </p>
  
  <pre><code>&lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th *ngFor="let col of cols"&gt;
                  {{col.header}}
                  &lt;input type="text"
                  [(ngModel)]=fields[col.value] 
                  (click)="selectColInput(col.value)"/&gt;
              &lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody *ngFor="let row of data | filter: fields:selectedInput"&gt;
          &lt;tr&gt;
              &lt;td *ngFor="let col of cols"&gt;{{row[col.value]}}&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>data-table.component.ts</p>
  
  <pre><code>import { ColumnsComponent } from './../columns/columns.component';
  import { Component, Input } from '@angular/core';
  import { FilterPipe } from './filter.pipe';
  
  @Component({
    selector: 'app-data-table',
    templateUrl: './data-table.component.html',
    styleUrls: ['./data-table.component.css']
  })
  
  export class DataTableComponent  {
  
    @Input() data
    cols: ColumnsComponent[] = []
    selectedInput: string = ''
    fields: any = {}
  
    selectColInput(col) {
      this.selectedInput = col
    }
  
    addColumn(column) {
      this.cols.push(column)
    }
  
  }
  </code></pre>
  
  <p>filter.pipe.ts</p>
  
  <pre><code>import { Pipe, PipeTransform } from '@angular/core';
  
  @Pipe({
    name: 'filter',
    pure: false
  })
  export class FilterPipe implements PipeTransform {
  
    transform(data: any, fields: any, selectedInput: any): any {
      if(!data) return;
  
      return data.filter( row =&gt; {
        if(row[selectedInput] !== null &amp;&amp; row[selectedInput] &amp;&amp; fields[selectedInput]){
          return row[selectedInput].toLowerCase().includes(fields[selectedInput].toLowerCase())
        }
        return true
      })
    }
  
  }
  </code></pre>