The following code for sorting table columns doesn't work. I get an Error message:
Error: $injector:unpr Unknown Provider
Unknown provider: orderbyFilterProvider <-
View:
<table class="table table-bordered table-hover">
<thead>
<tr>
<th><a href="" ng-click="reverse=!reverse;order('lname', reverse)">Lastname</a></th>
<th><a href="" ng-click="reverse=!reverse;order('fname', reverse)">Firstname</a></th>
<th><a href="" ng-click="reverse=!reverse;order('maxAge', reverse)">Age</a></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in filteredItems = (nameslist | orderBy:predicate)">
<td>{{ item.lname }}</td>
<td>{{ item.fname }}</td>
<td>{{ item.maxAge }}</td>
</tr>
</tbody>
</table>
Ctrl:
//Get request to REST API
$scope.nameslist = resService.getAll();
//sort function
var orderby = $filter('orderby');
$scope.order = function (predicate, reverse) {
$scope.nameslist = orderby($scope.nameslist, predicate, reverse);
};
/* default */
$scope.order('-maxAge', false);
resService:
...
return {
getAll: function () {
return requestService.name.query();
},
...
}
How could I modify the sort function?
You could use angular's built in orderBy filter, like so for reversing as well:
And the following order function:
The logic behind it is that sending an additional parameter true to the orderBy would reverse it, So we are just using this to reverse.
Also your error could have been because of this line:
which should be:
Here is the plunker that works:
http://plnkr.co/edit/JSoyoCkBwknlAggEwU59?p=preview