evoapp.directive('pagingToolbar', function ($filter) { return { restrict: 'AE', template: '', scope: { store: '=' }, link: function (scope, elem, attrs) { scope.currentPage = 1; scope.showPager = attrs.showPager ? attrs.showPager : true; scope.numberOfPages = function(){ var numberOfPages = 0; if(scope.store.total > 0) { numberOfPages = Math.ceil(scope.store.total/scope.store.limit); } else { numberOfPages = 0; } return numberOfPages; }; scope.move = function(direction) { if(direction == 'forward') { scope.store.start = scope.store.start + scope.store.limit; scope.currentPage = scope.currentPage + 1; } else { scope.store.start = scope.store.start - scope.store.limit; scope.currentPage = scope.currentPage - 1; } }; scope.$watch('store.start', function(newValue, oldValue){ if(newValue != oldValue && newValue != undefined) { scope.get(); } }); scope.$watch('store.total', function(newValue, oldValue){ if(!isNaN(newValue)) { var total = parseInt(newValue); if(total == 0) { scope.currentPage = 0; } else { scope.currentPage = scope.currentPage == 0 ? 1 : scope.currentPage; } } }); scope.get = function(){ scope.store.get(function(response, status, headers, config, items){ scope.currentPage = scope.store.start == 0 ? (items.length > 0 ? 1 : 0) : scope.currentPage; }); }; scope.showingInfo = function(){ var from = (scope.store.items.length == 0 ? 0 : (scope.store.start + 1)), to = (scope.store.start + scope.store.items.length); return $filter('stringFormat')('A mostrar {0} - {1} de {2}', [from, to, scope.store.total]); }; scope.pageInfo = function(){ var info = 'Página ' + scope.currentPage + ' de ' + scope.numberOfPages(); return info; }; } } });