app-paginator.directive.js
1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
(function() {
'use strict';
angular
.module('dialoga')
.directive('appPaginator', appPaginator);
/** @ngInject */
function appPaginator() {
/** @ngInject */
function AppPaginatorController($scope, $log) {
var vm = this;
vm.$scope = $scope;
vm.$log = $log;
vm.init();
vm.attachListeners();
$log.debug('AppPaginatorController');
}
AppPaginatorController.prototype.init = function() {
var vm = this;
vm.page = vm.page || 1;
vm.perPage = vm.perPage || 20;
vm.total = vm.total || 0;
vm.calcArrayPages();
};
AppPaginatorController.prototype.calcArrayPages = function() {
var vm = this;
if ((vm.total % vm.perPage) === 0) {
vm.pages = vm.total / vm.perPage;
} else {
vm.pages = (vm.total / vm.perPage) + 1;
}
vm.arraypages = new Array(Math.floor(vm.pages));
};
AppPaginatorController.prototype.attachListeners = function() {
var vm = this;
vm.$scope.$watch('vm.perPage', function() {
vm.calcArrayPages();
});
vm.$scope.$watch('vm.total', function() {
vm.calcArrayPages();
});
};
AppPaginatorController.prototype.showPage = function(pageIndex) {
var vm = this;
if (pageIndex < 1) {
pageIndex = 1;
}
if (pageIndex > vm.pages) {
pageIndex = vm.pages;
}
if (vm.changePage) {
vm.changePage({pageIndex: pageIndex});
}
};
var directive = {
restrict: 'E',
templateUrl: 'app/components/app-paginator/app-paginator.html',
scope: {
page: '=',
perPage: '=',
total: '=',
changePage: '&'
},
controller: AppPaginatorController,
controllerAs: 'vm',
bindToController: true
};
return directive;
}
})();