K
K
kachurinets2018-03-10 16:32:38
JavaScript
kachurinets, 2018-03-10 16:32:38

How to make a pipe to search results for the entire table?

I'm having trouble creating a pipe to look up values ​​across an entire table.
https://plnkr.co/edit/PSU0Hh?p=info
In my example, the filter only looks for one field. How to make a pipe to search for values ​​in the entire table?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-03-10
@kachurinets

Obviously, you need to iterate over all the fields, instead of checking for a specific one. For example like this :

class FilterPipe implements PipeTransform {
  transform(items: any[], term): any {
    const keys = items.length ? Object.keys(items[0]) : null;

    return term && keys
      ? items.filter(item => keys.some(key => item[key].toString().indexOf(term) !== -1))
      : items;
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question