Trying to filter data in *ngFor directive

后端 未结 3 1567
感动是毒
感动是毒 2020-12-21 23:00

I\'m trying to filter my array data on a table from Angular with the package w-ng5, but it\'s searching and it doesn\'t find nothing (blank list). This is the s

3条回答
  •  情歌与酒
    2020-12-21 23:26

    You can create a filter function that is called whenever your filter string changes.

    The code will look something like this:

      isMatch(item) {
        if (item instanceof Object) {
          return Object.keys(item).some((k) => this.isMatch(item[k]));
        } else {
          return item.toString().indexOf(this.filterString) > -1
        }
      }
    

    This will iterate through the object recursively, and look for any matches. While it's not necessary (seeing as your object structure is flat), it could be useful if your structure ever changes. Bear in mind it's case sensitive as it stands

    You then need to create a function that calls it, like so:

    onFilterChange() {
      this.filtered = this.invoices.filter((invoice) => this.isMatch(invoice));
    }
    

    And amend your input so that it refilters any time that the search string is changed

    
    

    Here is a Stackblitz demo

提交回复
热议问题