Commit a410b1873cf6e9cef731c3839de8bb8a1e7732f8
1 parent
a21e58e364
Exists in
master
and in
1 other branch
controlo cuando idLista viene null o undefined
Showing
1 changed file
with
16 additions
and
6 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 | 'idLista', | 7 | 'idLista', |
8 | 'focaBusquedaProductosService', | 8 | 'focaBusquedaProductosService', |
9 | function($filter, $scope, $uibModalInstance, idLista, focaBusquedaProductosService) { | 9 | function($filter, $scope, $uibModalInstance, idLista, focaBusquedaProductosService) { |
10 | focaBusquedaProductosService.getProductosByIdLista(idLista).then( | 10 | if(idLista) { |
11 | function(res) { | 11 | focaBusquedaProductosService.getProductosByIdLista(idLista).then( |
12 | $scope.productos = res.data; | 12 | function(res) { |
13 | $scope.search(); | 13 | $scope.productos = res.data; |
14 | } | 14 | $scope.search(); |
15 | ); | 15 | } |
16 | ); | ||
17 | } else { | ||
18 | focaBusquedaProductosService.getProductos().then( | ||
19 | function(res) { | ||
20 | $scope.productos = res.data; | ||
21 | $scope.search(); | ||
22 | } | ||
23 | ); | ||
24 | } | ||
25 | |||
16 | 26 | ||
17 | // pagination | 27 | // pagination |
18 | $scope.numPerPage = 10; | 28 | $scope.numPerPage = 10; |
19 | $scope.currentPage = 1; | 29 | $scope.currentPage = 1; |
20 | $scope.filteredProductos = []; | 30 | $scope.filteredProductos = []; |
21 | $scope.currentPageProductos = []; | 31 | $scope.currentPageProductos = []; |
22 | $scope.selectedProducto = -1; | 32 | $scope.selectedProducto = -1; |
23 | 33 | ||
24 | //METODOS | 34 | //METODOS |
25 | $scope.search = function() { | 35 | $scope.search = function() { |
26 | $scope.filteredProductos = $filter('filter')( | 36 | $scope.filteredProductos = $filter('filter')( |
27 | $scope.productos, | 37 | $scope.productos, |
28 | {$: $scope.filters} | 38 | {$: $scope.filters} |
29 | ); | 39 | ); |
30 | 40 | ||
31 | $scope.lastPage = Math.ceil( | 41 | $scope.lastPage = Math.ceil( |
32 | $scope.filteredProductos.length / $scope.numPerPage | 42 | $scope.filteredProductos.length / $scope.numPerPage |
33 | ); | 43 | ); |
34 | 44 | ||
35 | $scope.resetPage(); | 45 | $scope.resetPage(); |
36 | }; | 46 | }; |
37 | 47 | ||
38 | $scope.resetPage = function() { | 48 | $scope.resetPage = function() { |
39 | $scope.currentPage = 1; | 49 | $scope.currentPage = 1; |
40 | $scope.selectPage(1); | 50 | $scope.selectPage(1); |
41 | }; | 51 | }; |
42 | 52 | ||
43 | $scope.selectPage = function(page) { | 53 | $scope.selectPage = function(page) { |
44 | var start = (page - 1) * $scope.numPerPage; | 54 | var start = (page - 1) * $scope.numPerPage; |
45 | var end = start + $scope.numPerPage; | 55 | var end = start + $scope.numPerPage; |
46 | $scope.paginas = []; | 56 | $scope.paginas = []; |
47 | $scope.paginas = calcularPages(page); | 57 | $scope.paginas = calcularPages(page); |
48 | $scope.currentPageProductos = $scope.filteredProductos.slice(start, end); | 58 | $scope.currentPageProductos = $scope.filteredProductos.slice(start, end); |
49 | $scope.currentPage = page; | 59 | $scope.currentPage = page; |
50 | }; | 60 | }; |
51 | 61 | ||
52 | $scope.select = function(producto) { | 62 | $scope.select = function(producto) { |
53 | $uibModalInstance.close(producto); | 63 | $uibModalInstance.close(producto); |
54 | }; | 64 | }; |
55 | 65 | ||
56 | $scope.cancel = function() { | 66 | $scope.cancel = function() { |
57 | $uibModalInstance.dismiss('cancel'); | 67 | $uibModalInstance.dismiss('cancel'); |
58 | }; | 68 | }; |
59 | 69 | ||
60 | $scope.busquedaDown = function(key) { | 70 | $scope.busquedaDown = function(key) { |
61 | if (key === 40) { | 71 | if (key === 40) { |
62 | primera(key); | 72 | primera(key); |
63 | } | 73 | } |
64 | }; | 74 | }; |
65 | 75 | ||
66 | $scope.busquedaPress = function(key) { | 76 | $scope.busquedaPress = function(key) { |
67 | if (key === 13) { | 77 | if (key === 13) { |
68 | primera(key); | 78 | primera(key); |
69 | } | 79 | } |
70 | }; | 80 | }; |
71 | 81 | ||
72 | $scope.itemProducto = function(key) { | 82 | $scope.itemProducto = function(key) { |
73 | if (key === 38) { | 83 | if (key === 38) { |
74 | anterior(key); | 84 | anterior(key); |
75 | } | 85 | } |
76 | 86 | ||
77 | if (key === 40) { | 87 | if (key === 40) { |
78 | siguiente(key); | 88 | siguiente(key); |
79 | } | 89 | } |
80 | 90 | ||
81 | if (key === 37) { | 91 | if (key === 37) { |
82 | retrocederPagina(); | 92 | retrocederPagina(); |
83 | } | 93 | } |
84 | 94 | ||
85 | if (key === 39) { | 95 | if (key === 39) { |
86 | avanzarPagina(); | 96 | avanzarPagina(); |
87 | } | 97 | } |
88 | }; | 98 | }; |
89 | 99 | ||
90 | function calcularPages(paginaActual) { | 100 | function calcularPages(paginaActual) { |
91 | var paginas = []; | 101 | var paginas = []; |
92 | paginas.push(paginaActual); | 102 | paginas.push(paginaActual); |
93 | 103 | ||
94 | if (paginaActual - 1 > 1) { | 104 | if (paginaActual - 1 > 1) { |
95 | 105 | ||
96 | paginas.unshift(paginaActual - 1); | 106 | paginas.unshift(paginaActual - 1); |
97 | if (paginaActual - 2 > 1) { | 107 | if (paginaActual - 2 > 1) { |
98 | paginas.unshift(paginaActual - 2); | 108 | paginas.unshift(paginaActual - 2); |
99 | } | 109 | } |
100 | } | 110 | } |
101 | 111 | ||
102 | if (paginaActual + 1 < $scope.lastPage) { | 112 | if (paginaActual + 1 < $scope.lastPage) { |
103 | paginas.push(paginaActual + 1); | 113 | paginas.push(paginaActual + 1); |
104 | if (paginaActual + 2 < $scope.lastPage) { | 114 | if (paginaActual + 2 < $scope.lastPage) { |
105 | paginas.push(paginaActual + 2); | 115 | paginas.push(paginaActual + 2); |
106 | } | 116 | } |
107 | } | 117 | } |
108 | 118 | ||
109 | if (paginaActual !== 1) { | 119 | if (paginaActual !== 1) { |
110 | paginas.unshift(1); | 120 | paginas.unshift(1); |
111 | } | 121 | } |
112 | 122 | ||
113 | if (paginaActual !== $scope.lastPage) { | 123 | if (paginaActual !== $scope.lastPage) { |
114 | paginas.push($scope.lastPage); | 124 | paginas.push($scope.lastPage); |
115 | } | 125 | } |
116 | 126 | ||
117 | return paginas; | 127 | return paginas; |
118 | } | 128 | } |
119 | 129 | ||
120 | function primera() { | 130 | function primera() { |
121 | $scope.selectedProducto = 0; | 131 | $scope.selectedProducto = 0; |
122 | } | 132 | } |
123 | 133 | ||
124 | function anterior() { | 134 | function anterior() { |
125 | if ($scope.selectedProducto === 0 && $scope.currentPage > 1) { | 135 | if ($scope.selectedProducto === 0 && $scope.currentPage > 1) { |
126 | retrocederPagina(); | 136 | retrocederPagina(); |
127 | } else { | 137 | } else { |
128 | $scope.selectedProducto--; | 138 | $scope.selectedProducto--; |
129 | } | 139 | } |
130 | } | 140 | } |
131 | 141 | ||
132 | function siguiente() { | 142 | function siguiente() { |
133 | if ($scope.selectedProducto < $scope.currentPageProductos.length - 1 ) { | 143 | if ($scope.selectedProducto < $scope.currentPageProductos.length - 1 ) { |
134 | $scope.selectedProducto++; | 144 | $scope.selectedProducto++; |
135 | } else { | 145 | } else { |
136 | avanzarPagina(); | 146 | avanzarPagina(); |
137 | } | 147 | } |
138 | } | 148 | } |
139 | 149 | ||
140 | function retrocederPagina() { | 150 | function retrocederPagina() { |
141 | if ($scope.currentPage > 1) { | 151 | if ($scope.currentPage > 1) { |
142 | $scope.selectPage($scope.currentPage - 1); | 152 | $scope.selectPage($scope.currentPage - 1); |
143 | $scope.selectedProducto = $scope.numPerPage - 1; | 153 | $scope.selectedProducto = $scope.numPerPage - 1; |
144 | } | 154 | } |
145 | } | 155 | } |
146 | 156 | ||
147 | function avanzarPagina() { | 157 | function avanzarPagina() { |
148 | if ($scope.currentPage < $scope.lastPage) { | 158 | if ($scope.currentPage < $scope.lastPage) { |
149 | $scope.selectPage($scope.currentPage + 1); | 159 | $scope.selectPage($scope.currentPage + 1); |
150 | $scope.selectedProducto = 0; | 160 | $scope.selectedProducto = 0; |
151 | } | 161 | } |
152 | } | 162 | } |
153 | } | 163 | } |
154 | ] | 164 | ] |
155 | ); | 165 | ); |
156 | 166 |