Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 4a57b15

Browse files
zainengineercaitp
authored andcommitted
docs(orderBy): add example of directly calling $filter('orderBy')
It's not a bad example of sorting fields in a table, which is something people are frequently wanting to do. So I say, LGTM! ~caitp, 1988-2014 Closes #7602
1 parent 16aa3af commit 4a57b15

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/ng/filter/orderBy.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,51 @@
6161
</div>
6262
</file>
6363
</example>
64+
*
65+
* It's also possible to call the orderBy filter manually, by injecting `$filter`, retrieving the
66+
* filter routine with `$filter('orderBy')`, and calling the returned filter routine with the
67+
* desired parameters.
68+
*
69+
* Example:
70+
*
71+
* @example
72+
<example>
73+
<file name="index.html">
74+
<div ng-controller="Ctrl">
75+
<table class="friend">
76+
<tr>
77+
<th><a href="" ng-click="reverse=false;order('name', false)">Name</a>
78+
(<a href="" ng-click="order('-name',false)">^</a>)</th>
79+
<th><a href="" ng-click="reverse=!reverse;order('phone', reverse)">Phone Number</a></th>
80+
<th><a href="" ng-click="reverse=!reverse;order('age',reverse)">Age</a></th>
81+
</tr>
82+
<tr ng-repeat="friend in friends">
83+
<td>{{friend.name}}</td>
84+
<td>{{friend.phone}}</td>
85+
<td>{{friend.age}}</td>
86+
</tr>
87+
</table>
88+
</div>
89+
</file>
90+
91+
<file name="script.js">
92+
function Ctrl($scope, $filter) {
93+
var orderBy = $filter('orderBy');
94+
$scope.friends = [
95+
{ name: 'John', phone: '555-1212', age: 10 },
96+
{ name: 'Mary', phone: '555-9876', age: 19 },
97+
{ name: 'Mike', phone: '555-4321', age: 21 },
98+
{ name: 'Adam', phone: '555-5678', age: 35 },
99+
{ name: 'Julie', phone: '555-8765', age: 29 }
100+
];
101+
102+
$scope.order = function(predicate, reverse) {
103+
$scope.friends = orderBy($scope.friends, predicate, reverse);
104+
};
105+
$scope.order('-age',false);
106+
}
107+
</file>
108+
</example>
64109
*/
65110
orderByFilter.$inject = ['$parse'];
66111
function orderByFilter($parse){

0 commit comments

Comments
 (0)