Answer the question
In order to leave comments, you need to log in
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' } } ];
<table class="table table-hover table-bordered">
<thead>
<tr>
<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>
</thead>
<tbody>
<tr ng-repeat="item in items" class="gradeX">
<td>
{{ item.name }}
</td>
<td>
{{ item.phone.type }} : {{ item.phone.number }}
</td>
</tr>
</tbody>
</table>
<a href="" ng-click="reverse=!reverse;order('name', reverse)">Name</a>
var orderBy = $filter('orderBy');
$scope.order = function(predicate, reverse) {
$scope.items = orderBy($scope.items, predicate, reverse);
};
reverse=!reverse;order('name', reverse)
reverse=!reverse;order('phone.number', reverse)
Answer the question
In order to leave comments, you need to log in
Try this. In HTML:
And in the title
I checked it on the fiddle. It seems to work.
jsfiddle.net/Serhioromano/hrg0k5es/2
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question