1

Angular.io states that filtering and sorting should not be done in a Pipe due to the amount of calls per-second it will make, however, in the community, there is no shortage of examples and tutorials on doing it with a Pipe.

What is the ideal way to implement a pre/post-sort in a template, without mutating the original data and still have a reference to the original data?

I am coming from Vue.js, my first thought is a computed property, which may be close to a 'get' in angular??, but not sure.

steve
  • 1,786
  • 1
  • 15
  • 29
  • One possibility is to put the data in an observable (BehaviorSubject) and build another observable from that where you map to filter/sort function, and use that observable in the template with the async pipe – ShamPooSham Jan 12 '23 at 20:21
  • Also, creating pure filter pipes isn't bad in itself, it's just not very convenient since it won't update when the array is updated. But if you overwrite the property with a new array, it will. Or you can use ChangeDetectionRef – ShamPooSham Jan 12 '23 at 20:36
  • @steve, you have two arrays `data` and `dataFiltered` (which is the array you use to loop). Each time you filter or sort, recalcule the `dataFiltered` – Eliseo Jan 12 '23 at 21:43
  • Here it is: https://stackoverflow.com/questions/35158817/orderby-pipe-issue – Alexis Deprez Jan 12 '23 at 22:27

0 Answers0