Skip to content

Instantly share code, notes, and snippets.

@menacestudio
Created March 13, 2015 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save menacestudio/b26859a53ff279e30a98 to your computer and use it in GitHub Desktop.
Save menacestudio/b26859a53ff279e30a98 to your computer and use it in GitHub Desktop.
An AngularJS directive to add sorting capability to table headers.
sortableGrid.$inject = [];
function sortableGrid() {
return <ng.IDirective>{
restrict: 'A',
scope: {
order: '=',
by: '=',
reverse: '='
},
transclude: true,
template: '<a ng-click="sort()">' +
'<span ng-transclude></span>' +
'</a>&nbsp;<i class="glyphicon" ng-class="{\'fa fa-sort-asc\' : order===by && !reverse, \'fa fa-sort-desc\' : order===by && reverse}"></i>',
link: link
}
function link($scope, $element, $attrs) {
$scope.sort = function () {
if ($scope.order === $scope.by) {
$scope.reverse = !$scope.reverse
} else {
$scope.by = $scope.order;
$scope.reverse = false;
}
}
}
}
angular.module('app').directive('sortableGrid', sortableGrid);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment