Commit 6551d4553e1cb3869d0887da07fa0976268f9d89
1 parent
c8341653fd
Exists in
master
fix retroceder pagina con flecha para arriba
Showing
1 changed file
with
1 additions
and
1 deletions
Show diff stats
src/js/controller.js
1 | angular.module('focaBusquedaProductos') | 1 | angular.module('focaBusquedaProductos') |
2 | .controller('modalBusquedaProductosCtrl', | 2 | .controller('modalBusquedaProductosCtrl', |
3 | [ | 3 | [ |
4 | '$filter', | 4 | '$filter', |
5 | '$scope', | 5 | '$scope', |
6 | '$uibModalInstance', | 6 | '$uibModalInstance', |
7 | 'focaBusquedaProductosService', | 7 | 'focaBusquedaProductosService', |
8 | function($filter, $scope, $uibModalInstance, focaBusquedaProductosService) { | 8 | function($filter, $scope, $uibModalInstance, focaBusquedaProductosService) { |
9 | focaBusquedaProductosService.getProductos().then( | 9 | focaBusquedaProductosService.getProductos().then( |
10 | function(res) { | 10 | function(res) { |
11 | $scope.productos = res.data; | 11 | $scope.productos = res.data; |
12 | $scope.search(); | 12 | $scope.search(); |
13 | } | 13 | } |
14 | ); | 14 | ); |
15 | 15 | ||
16 | // pagination | 16 | // pagination |
17 | $scope.numPerPage = 10; | 17 | $scope.numPerPage = 10; |
18 | $scope.currentPage = 1; | 18 | $scope.currentPage = 1; |
19 | $scope.filteredProductos = []; | 19 | $scope.filteredProductos = []; |
20 | $scope.currentPageProductos = []; | 20 | $scope.currentPageProductos = []; |
21 | $scope.selectedProducto = -1; | 21 | $scope.selectedProducto = -1; |
22 | 22 | ||
23 | //METODOS | 23 | //METODOS |
24 | $scope.search = function() { | 24 | $scope.search = function() { |
25 | $scope.filteredProductos = $filter('filter')($scope.productos, {$: $scope.filters}); | 25 | $scope.filteredProductos = $filter('filter')($scope.productos, {$: $scope.filters}); |
26 | $scope.lastPage = Math.ceil($scope.filteredProductos.length / $scope.numPerPage); | 26 | $scope.lastPage = Math.ceil($scope.filteredProductos.length / $scope.numPerPage); |
27 | $scope.resetPage(); | 27 | $scope.resetPage(); |
28 | } | 28 | } |
29 | 29 | ||
30 | $scope.resetPage = function() { | 30 | $scope.resetPage = function() { |
31 | $scope.currentPage = 1; | 31 | $scope.currentPage = 1; |
32 | $scope.selectPage(1); | 32 | $scope.selectPage(1); |
33 | } | 33 | } |
34 | 34 | ||
35 | $scope.selectPage = function(page) { | 35 | $scope.selectPage = function(page) { |
36 | var start = (page - 1) * $scope.numPerPage; | 36 | var start = (page - 1) * $scope.numPerPage; |
37 | var end = start + $scope.numPerPage; | 37 | var end = start + $scope.numPerPage; |
38 | $scope.paginas = []; | 38 | $scope.paginas = []; |
39 | $scope.paginas = calcularPages(page); | 39 | $scope.paginas = calcularPages(page); |
40 | $scope.currentPageProductos = $scope.filteredProductos.slice(start, end); | 40 | $scope.currentPageProductos = $scope.filteredProductos.slice(start, end); |
41 | $scope.currentPage = page; | 41 | $scope.currentPage = page; |
42 | } | 42 | } |
43 | 43 | ||
44 | $scope.select = function(producto) { | 44 | $scope.select = function(producto) { |
45 | $uibModalInstance.close(producto); | 45 | $uibModalInstance.close(producto); |
46 | } | 46 | } |
47 | 47 | ||
48 | $scope.cancel = function() { | 48 | $scope.cancel = function() { |
49 | $uibModalInstance.dismiss('cancel'); | 49 | $uibModalInstance.dismiss('cancel'); |
50 | } | 50 | } |
51 | 51 | ||
52 | $scope.busquedaDown = function(key) { | 52 | $scope.busquedaDown = function(key) { |
53 | if (key === 40) { | 53 | if (key === 40) { |
54 | primera(key); | 54 | primera(key); |
55 | } | 55 | } |
56 | } | 56 | } |
57 | 57 | ||
58 | $scope.busquedaPress = function(key) { | 58 | $scope.busquedaPress = function(key) { |
59 | if (key === 13) { | 59 | if (key === 13) { |
60 | primera(key); | 60 | primera(key); |
61 | } | 61 | } |
62 | } | 62 | } |
63 | 63 | ||
64 | $scope.itemProducto = function(key) { | 64 | $scope.itemProducto = function(key) { |
65 | if (key == 38) { | 65 | if (key == 38) { |
66 | anterior(key); | 66 | anterior(key); |
67 | } | 67 | } |
68 | 68 | ||
69 | if (key == 40) { | 69 | if (key == 40) { |
70 | siguiente(key); | 70 | siguiente(key); |
71 | } | 71 | } |
72 | 72 | ||
73 | if (key == 37) { | 73 | if (key == 37) { |
74 | retrocederPagina(); | 74 | retrocederPagina(); |
75 | } | 75 | } |
76 | 76 | ||
77 | if (key == 39) { | 77 | if (key == 39) { |
78 | avanzarPagina(); | 78 | avanzarPagina(); |
79 | } | 79 | } |
80 | } | 80 | } |
81 | 81 | ||
82 | function calcularPages(paginaActual) { | 82 | function calcularPages(paginaActual) { |
83 | var paginas = []; | 83 | var paginas = []; |
84 | paginas.push(paginaActual); | 84 | paginas.push(paginaActual); |
85 | 85 | ||
86 | if (paginaActual - 1 > 1) { | 86 | if (paginaActual - 1 > 1) { |
87 | 87 | ||
88 | paginas.unshift(paginaActual - 1); | 88 | paginas.unshift(paginaActual - 1); |
89 | if (paginaActual - 2 > 1) { | 89 | if (paginaActual - 2 > 1) { |
90 | paginas.unshift(paginaActual - 2); | 90 | paginas.unshift(paginaActual - 2); |
91 | } | 91 | } |
92 | } | 92 | } |
93 | 93 | ||
94 | if (paginaActual + 1 < $scope.lastPage) { | 94 | if (paginaActual + 1 < $scope.lastPage) { |
95 | paginas.push(paginaActual + 1); | 95 | paginas.push(paginaActual + 1); |
96 | if (paginaActual + 2 < $scope.lastPage) { | 96 | if (paginaActual + 2 < $scope.lastPage) { |
97 | paginas.push(paginaActual + 2); | 97 | paginas.push(paginaActual + 2); |
98 | } | 98 | } |
99 | } | 99 | } |
100 | 100 | ||
101 | if (paginaActual !== 1) { | 101 | if (paginaActual !== 1) { |
102 | paginas.unshift(1); | 102 | paginas.unshift(1); |
103 | } | 103 | } |
104 | 104 | ||
105 | if (paginaActual !== $scope.lastPage) { | 105 | if (paginaActual !== $scope.lastPage) { |
106 | paginas.push($scope.lastPage); | 106 | paginas.push($scope.lastPage); |
107 | } | 107 | } |
108 | 108 | ||
109 | return paginas; | 109 | return paginas; |
110 | } | 110 | } |
111 | 111 | ||
112 | function primera(key) { | 112 | function primera(key) { |
113 | $scope.selectedProducto = 0; | 113 | $scope.selectedProducto = 0; |
114 | } | 114 | } |
115 | 115 | ||
116 | function anterior(key) { | 116 | function anterior(key) { |
117 | if ($scope.selectedProducto === 0) { | 117 | if ($scope.selectedProducto === 0) { |
118 | anteriorPagina(); | 118 | retrocederPagina(); |
119 | } else { | 119 | } else { |
120 | $scope.selectedProducto--; | 120 | $scope.selectedProducto--; |
121 | } | 121 | } |
122 | } | 122 | } |
123 | 123 | ||
124 | function siguiente(key) { | 124 | function siguiente(key) { |
125 | if ($scope.selectedProducto < $scope.currentPageProductos.length - 1 ) { | 125 | if ($scope.selectedProducto < $scope.currentPageProductos.length - 1 ) { |
126 | $scope.selectedProducto++; | 126 | $scope.selectedProducto++; |
127 | } else { | 127 | } else { |
128 | avanzarPagina(); | 128 | avanzarPagina(); |
129 | } | 129 | } |
130 | } | 130 | } |
131 | 131 | ||
132 | function retrocederPagina() { | 132 | function retrocederPagina() { |
133 | if ($scope.currentPage > 1) { | 133 | if ($scope.currentPage > 1) { |
134 | $scope.selectPage($scope.currentPage - 1); | 134 | $scope.selectPage($scope.currentPage - 1); |
135 | $scope.selectedProducto = $scope.numPerPage - 1; | 135 | $scope.selectedProducto = $scope.numPerPage - 1; |
136 | } | 136 | } |
137 | } | 137 | } |
138 | 138 | ||
139 | function avanzarPagina() { | 139 | function avanzarPagina() { |
140 | if ($scope.currentPage < $scope.lastPage) { | 140 | if ($scope.currentPage < $scope.lastPage) { |
141 | $scope.selectPage($scope.currentPage + 1); | 141 | $scope.selectPage($scope.currentPage + 1); |
142 | $scope.selectedProducto = 0; | 142 | $scope.selectedProducto = 0; |
143 | } | 143 | } |
144 | } | 144 | } |
145 | } | 145 | } |
146 | ] | 146 | ] |
147 | ) | 147 | ) |
148 | 148 |