Dmitry Arushanov2014-08-11 16:41:44
Dmitry Arushanov, 2014-08-11 16:41:44

AngularJs sort by nested objects?

Good day to all.
Let's say we have an array of data like this:

$scope.items = [  
{ name :  'Name1',  phone : { type : 'mob', number : '1234' } },  
{ name :  'Name2',  phone:  { type : 'mob', number : '545234' } } ];

Well, on the view, I display them like this
<table class="table table-hover table-bordered">
            <th><a href="" ng-click="reverse=!reverse;order('name', reverse)">Name</a></th>
            <th><a href="" ng-click="reverse=!reverse;order('????', reverse)">Phone</a></th>
        <tr ng-repeat="item in items" class="gradeX">
               {{ item.name }}  
              {{ item.phone.type }} : {{ item.phone.number }}


Actually sorting is done when you click on the title
<a href="" ng-click="reverse=!reverse;order('name', reverse)">Name</a>

and in the controller this code
var orderBy = $filter('orderBy');

      $scope.order = function(predicate, reverse) {
          $scope.items = orderBy($scope.items, predicate, reverse);

So when I just sort by name
reverse=!reverse;order('name', reverse)
Everything works. But how to sort out by phone (by number) .. ???
probyval so
reverse=!reverse;order('phone.number', reverse)
But it does not sort normally ... ((

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Sergey Romanov, 2014-08-13

Try this. In HTML:
And in the title
I checked it on the fiddle. It seems to work.

apasen, 2014-08-11

Maybe not sure, need to use a function to sort.
In place of predicate

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question