noobcoder noobcoder - 4 months ago 8
AngularJS Question

Angular orderBy to also order the original array

New to Angular, I ham using angular's

orderBy
on a column header as the code snippet mentioned in my earlier post: AngularJS orderby not working as expected

Now in my controller, the array
vm.albums
is still going to maintain its original order as it came from the backend and orderBy directive is just going to order the DOM as per the sorting criteria. I want to know how do I extract the top row after every re-ordering from the DOM ? This is needed for me to display another set of details depending on which the first row in the table is. Any clue ?

Answer

You can use filters from javascript $filter('orderBy')(collection, expression, reverse, comparator).

vm.top = $filter('orderBy')(vm.albums, 'data.title')[0]

This will sort albums by data.title and extract top row.

Comments