mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-04-24 03:55:49 +08:00
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
This commit is contained in:
committed by
Caitlin Potter
parent
16aa3aff51
commit
4a57b1501e
@@ -61,6 +61,51 @@
|
||||
</div>
|
||||
</file>
|
||||
</example>
|
||||
*
|
||||
* It's also possible to call the orderBy filter manually, by injecting `$filter`, retrieving the
|
||||
* filter routine with `$filter('orderBy')`, and calling the returned filter routine with the
|
||||
* desired parameters.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* @example
|
||||
<example>
|
||||
<file name="index.html">
|
||||
<div ng-controller="Ctrl">
|
||||
<table class="friend">
|
||||
<tr>
|
||||
<th><a href="" ng-click="reverse=false;order('name', false)">Name</a>
|
||||
(<a href="" ng-click="order('-name',false)">^</a>)</th>
|
||||
<th><a href="" ng-click="reverse=!reverse;order('phone', reverse)">Phone Number</a></th>
|
||||
<th><a href="" ng-click="reverse=!reverse;order('age',reverse)">Age</a></th>
|
||||
</tr>
|
||||
<tr ng-repeat="friend in friends">
|
||||
<td>{{friend.name}}</td>
|
||||
<td>{{friend.phone}}</td>
|
||||
<td>{{friend.age}}</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
</file>
|
||||
|
||||
<file name="script.js">
|
||||
function Ctrl($scope, $filter) {
|
||||
var orderBy = $filter('orderBy');
|
||||
$scope.friends = [
|
||||
{ name: 'John', phone: '555-1212', age: 10 },
|
||||
{ name: 'Mary', phone: '555-9876', age: 19 },
|
||||
{ name: 'Mike', phone: '555-4321', age: 21 },
|
||||
{ name: 'Adam', phone: '555-5678', age: 35 },
|
||||
{ name: 'Julie', phone: '555-8765', age: 29 }
|
||||
];
|
||||
|
||||
$scope.order = function(predicate, reverse) {
|
||||
$scope.friends = orderBy($scope.friends, predicate, reverse);
|
||||
};
|
||||
$scope.order('-age',false);
|
||||
}
|
||||
</file>
|
||||
</example>
|
||||
*/
|
||||
orderByFilter.$inject = ['$parse'];
|
||||
function orderByFilter($parse){
|
||||
|
||||
Reference in New Issue
Block a user