物料表,每列均具有过滤功能

时间:2019-05-16 09:12:12

标签: angular filter material material-table

我正在用材料实现通用表。

问题在于,如何创建过滤器方法,该方法仅过滤自己的列。

代码看起来像这样:

<ng-container *ngIf="dataSource">
  <table
    mat-table
    [dataSource]="dataSource"
    class="mat-elevation-z8"
    class="generic-table"
  >
    <ng-container *ngFor="let colName of displayedColumns">
      <ng-container matColumnDef="{{ colName }}">
        <th mat-header-cell *matHeaderCellDef class="{{ headerClasses }}">
          <ng-container *ngIf="!isFilterAvailable">
            {{ colName }}
          </ng-container>
          <ng-container *ngIf="isFilterAvailable">
            <mat-form-field class="filter" floatLabel="never">
              <mat-label>Search</mat-label>
              <input matInput (keyup)="doFilter($event.target.value, colName)" />
            </mat-form-field>
          </ng-container>
        </th>
        <td
          mat-cell
          *matCellDef="let cellData"
          class="{{
            cellData[colName]
              | customPipeChecks: colName:cellData:customCssFunction
          }}"
          innerHtml="{{
            cellData[colName]
              | customPipeChecks: colName:cellData:customValueFunction
          }} "
        ></td>
      </ng-container>
    </ng-container>

    <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
    -->
    <tr mat-row *matRowDef="let row; columns: displayedColumns"></tr>
  </table>
</ng-container>

和ts。文件如下所示:

import { filter } from "rxjs/operators";
import { GenericTableData } from "@app/models/generic-table-data.model";
import { Component, Input, OnInit } from "@angular/core";
import * as _ from "underscore";
import { DataSource } from "@angular/cdk/table";
import { MatTableDataSource } from "@angular/material";
import { FormControl } from "@angular/forms";

@Component({
  selector: "app-generic-table",
  templateUrl: "./generic-table.component.html",
  styleUrls: ["./generic-table.component.scss"]
})
export class GenericTableComponent implements OnInit {
  @Input() isFilterAvailable: Boolean;
  @Input() tableData: GenericTableData;
  @Input() displayedColumns: string[];
  @Input() headerClasses = "";
  @Input() customValueFunction: (
    elemValue: string,
    elemKey: string,
    elem: {}
  ) => string;
  @Input() customCssFunction: (
    elemValue: string,
    elemKey: string,
    elem: {}
  ) => string;
  dataSource = new MatTableDataSource();
  filters = {};
  constructor() {}

  ngOnInit() {
    if (
      !this.displayedColumns &&
      this.tableData.rows.length > 0 &&
      !this.isFilterAvailable
    ) {
      this.displayedColumns = _.keys(_.first(this.tableData.rows));
    } else {
      this.dataSource.data = this.tableData.rows;
      this.initFilterValues(_.keys(_.first(this.tableData.rows)));
      this.dataSource.filterPredicate = this.createFilter();
    }
    this.dataSource.data = this.tableData.rows;
  }

  initFilterValues(values) {
    values.forEach(key => {
      this.filters[key] = "";
    });
  }

  doFilter(filterValue: string, column: string) {
    this.filters[column] = filterValue.trim().toLocaleLowerCase();
    this.dataSource.filter = JSON.stringify(filter);
    if (this.dataSource.paginator) {
      this.dataSource.paginator.firstPage();
    }
  }

  createFilter(): (data: any, filter: string) => boolean {
    let filterFunction = function(data, filter): boolean {
      console.log("Function excuted");
      let searchTerms = JSON.parse(filter);

      data.forEach(function(key, value) {});
      return (
       true;
    };
    return filterFunction;
  }
}

我对已定义列的过滤器的解决方案感到困惑,但该表无法运行方法createFilter()。

我已经调试了。问题是函数createFilter将仅运行一次,如果我在过滤器中放一些东西,则函数createFilter()将不会运行。

如果我在搜索输入字段中添加一些内容并在功能filterFunction中设置断点,则不会发生任何事情。

在线我找到了这个例子:

https://stackblitz.com/edit/angular-hbakxo-xctfqy?file=app%2Ftable-filtering-example.ts

在此示例中,它也使用相同的方法。

有人可以告诉我,我做错了什么。

最好的问候,

狮子座

0 个答案:

没有答案