I have a list with items, and as you can see in the following plunkr, I'm trying to sort the table, using standard orderBy
filter:
<tr ng-repeat="item in list | orderBy: sorting.field : sorting.reverse">
<!-- some code goes here -->
</tr>
See the plunker or snippet below:
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.list = [{
id: 1,
title: 'test 1',
reactions: {
'like': 7,
'share': 20,
'comment': 100
}
}, {
id: 2,
title: 'test 2',
reactions: {
'like': 5,
'share': 10,
}
}, {
id: 3,
title: 'test 3',
reactions: {
'like': 4,
'share': 1,
}
}, {
id: 4,
title: 'test 4',
reactions: {
'like': 4,
'share': 1,
'comment': 2
}
}];
$scope.sorting = {
field: 'id',
reverse: false
};
});
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script data-require="angular.js@1.3.x" src="https://code.angularjs.org/1.3.5/angular.js" data-semver="1.3.5"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<div>
<table>
<tr>
<th ng-click="sorting.field = 'id'; sorting.reverse = ! sorting.reverse;">ID</th>
<th ng-click="sorting.field= 'title'; sorting.reverse = ! sorting.reverse;">Title</th>
<th ng-click="sorting.field = 'reactions.like'; sorting.reverse = ! sorting.reverse;">Likes</th>
<th ng-click="sorting.field = 'reactions.share'; sorting.reverse = ! sorting.reverse;">Shares</th>
<th ng-click="sorting.field = 'reactions.comment'; sorting.reverse = ! sorting.reverse;">Comments</th>
</tr>
<tr ng-repeat="item in list | orderBy: sorting.field : sorting.reverse">
<td>
{{item.id}}
</td>
<td>
{{item.title}}
</td>
<td>
{{item.reactions.like}}
</td>
<td>
{{item.reactions.share}}
</td>
<td>
{{item.reactions.comment}}
</td>
</tr>
</table>
<div class="hint">
<p>* Click on table heading in order to sort results.</p>
</div>
</div>
</body>
</html>
The problem comes when some properties are missing (n.b. - can't handle this, as the data comes from external API), take a look at "Comments" column and you'll see what I'm talking about. The data is not sorted as expected.
Yes, I tried everything from this thread, but nothing seems to work for me.