Dynamic orderBy in AngularJS
Update orderBy:myCalculatedValueFunction
to something like orderBy:dynamicOrderFunction
:
ERRONEOUS
$scope.dynamicOrderFunction = function() { if (orderByString) { return '-creationDate'; } else { return myCalculatedValueFunction; } }
orderBy
also has a 3rd property that accepts a boolean and will reverse orderBy when true
. (orderBy:dynamicOrderFunction:reverseOrder
where $scope.reverseOrder = true; // or false
)
edit
You will actually run into issues trying to switch orderBy between a string a function this way. Checkout out this jsfiddle for a working dynamic order function.
$scope.dynamicOrder = function(user) {
var order = 0;
switch ($scope.order.field) {
case 'gender':
order = gender_order[user.gender];
break;
default:
order = user[$scope.order.field];
}
return order;
}