简体   繁体   中英

filterPredicate from material table was not working in angular app

I want to implement a customer filter for my material table, but as I debugged and set a breakpoint in this customer filter function. but it was not working.

the code is such like this:


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;
    this.dataSource.filterPredicate = (data, filter) => {
      console.log(data + "hier hier hier");
      return true;
    };
  }

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

  doFilter(filterValue: string, column: string) {
    this.filters[column] = filterValue.trim().toLocaleLowerCase();
    this.dataSource.filter = JSON.stringify(this.filters);
    this.dataSource.filterPredicate = (data, filter) => {
      console.log(data + "asdfasdfa");
      return true;
    };
  }

  createFilter() {
    console.log("create asdfasdfasdfasdfasdf");
    const filterFunction = function(data, filter): boolean {
      console.log("Function excuted" + data);
      let searchTerms = JSON.parse(filter);
      return true;
    };
    return filterFunction;
  }
}

in HTML there is a input field, if I tip something for search, doFilter will be run, but the filterFunktion will show data only the fist row from all data

Best Regards,

Leo

[SOLVED] I'm seeing the same issue.. the filter predicate is set but never gets called.. Verified by putting some console.log inside. I rewrote the filter predicate as well. [EDIT] I solved this by setting the predicate just before applying the filter..not a solution but good workaround for now

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM