Skip to content

Instantly share code, notes, and snippets.

@kanwei
Created October 22, 2013 16:03
Show Gist options
  • Save kanwei/7103360 to your computer and use it in GitHub Desktop.
Save kanwei/7103360 to your computer and use it in GitHub Desktop.
Tourbuzz paginator
angular.module("tourbuzz.paginator", [])
.directive "paginator", ($parse) ->
return {
template: """<div ng-transclude></div>"""
transclude: true
controller: ($scope) ->
$scope.state = {page: 1, all: null, perPage: 10}
$scope.nextPage = -> $scope.state.page = parseInt($scope.state.page) + 1
$scope.prevPage = -> $scope.state.page = parseInt($scope.state.page) - 1
$scope.$watch("state" , (scope) ->
$scope.page = $scope.state.page || 1
$scope.numPages = Math.ceil($scope.state.all.length / 10) if $scope.state.all?
$scope.range = _.range(1, Math.ceil($scope.state.all.length / 10)) if $scope.state.all?
$scope.currentPage = $scope.state.all?.slice( ($scope.page - 1) * $scope.state.perPage, $scope.page * $scope.state.perPage)
, true)
link: (scope, element, attrs) ->
scope.state.perPage = attrs.paginatorPerPage if attrs.paginatorPerPage?
scope.$watch(attrs.paginator, ->
scope.state.page = 1
scope.state.all = $parse(attrs.paginator)(scope)
, true)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment