Commit e7f7c2656f9ca7c56ce67ab7f8440e4be7d53f20
1 parent
eff99c353a
Exists in
master
pequeño error
Showing
1 changed file
with
9 additions
and
7 deletions
Show diff stats
src/js/controller.js
1 | angular.module('focaModalProveedor') | 1 | angular.module('focaModalProveedor') |
2 | .controller('focaModalProveedorCtrl', [ | 2 | .controller('focaModalProveedorCtrl', [ |
3 | '$filter', | 3 | '$filter', |
4 | '$scope', | 4 | '$scope', |
5 | '$uibModalInstance', | 5 | '$uibModalInstance', |
6 | 'focaModalProveedorService', | 6 | 'focaModalProveedorService', |
7 | function ($filter, $scope, $uibModalInstance, focaModalProveedorService) { | 7 | function ($filter, $scope, $uibModalInstance, focaModalProveedorService) { |
8 | 8 | ||
9 | $scope.filters = ''; | 9 | $scope.filters = ''; |
10 | $scope.primerBusqueda = false; | 10 | $scope.primerBusqueda = false; |
11 | // pagination | 11 | // pagination |
12 | $scope.numPerPage = 10; | 12 | $scope.numPerPage = 10; |
13 | $scope.currentPage = 1; | 13 | $scope.currentPage = 1; |
14 | $scope.filteredProveedores = []; | 14 | $scope.filteredProveedores = []; |
15 | $scope.currentPageProveedores = []; | 15 | $scope.currentPageProveedores = []; |
16 | $scope.selectedProveedores = -1; | 16 | $scope.selectedProveedores = -1; |
17 | 17 | ||
18 | $scope.busquedaPress = function(key) { | 18 | $scope.busquedaPress = function(key) { |
19 | if (key === 13) { | 19 | if (key === 13) { |
20 | $scope.searchLoading = true; | 20 | $scope.searchLoading = true; |
21 | var json = { | 21 | var json = { |
22 | razonCuitCod: $scope.filters | 22 | razonCuitCod: $scope.filters |
23 | }; | 23 | }; |
24 | focaModalProveedorService.getProveedores(json).then( | 24 | focaModalProveedorService.getProveedores(json).then( |
25 | function(res) { | 25 | function(res) { |
26 | for (var i = res.data.length - 1; i >= 0; i--) { | 26 | for (var i = res.data.length - 1; i >= 0; i--) { |
27 | if (res.data[i].COD === 0) { | 27 | if (res.data[i].COD === 0) { |
28 | delete res.data[i]; | 28 | delete res.data[i]; |
29 | } | 29 | } |
30 | } | 30 | } |
31 | $scope.primerBusqueda = true; | 31 | $scope.primerBusqueda = true; |
32 | $scope.proveedores = res.data; | 32 | $scope.proveedores = res.data; |
33 | $scope.search(); | 33 | $scope.search(); |
34 | primera(); | 34 | primera(); |
35 | $scope.searchLoading = false; | 35 | $scope.searchLoading = false; |
36 | }); | 36 | }); |
37 | } | 37 | } |
38 | }; | 38 | }; |
39 | 39 | ||
40 | $scope.search = function () { | 40 | $scope.search = function () { |
41 | $scope.filteredProveedores = $filter('filter')( | 41 | if($scope.primerBusqueda) { |
42 | $scope.proveedores, {$: $scope.filters} | 42 | $scope.filteredProveedores = $filter('filter')( |
43 | ); | 43 | $scope.proveedores, {$: $scope.filters} |
44 | ); | ||
44 | 45 | ||
45 | $scope.lastPage = Math.ceil( | 46 | $scope.lastPage = Math.ceil( |
46 | $scope.filteredProveedores.length / $scope.numPerPage | 47 | $scope.filteredProveedores.length / $scope.numPerPage |
47 | ); | 48 | ); |
48 | 49 | ||
49 | $scope.resetPage(); | 50 | $scope.resetPage(); |
51 | } | ||
50 | }; | 52 | }; |
51 | 53 | ||
52 | $scope.resetPage = function () { | 54 | $scope.resetPage = function () { |
53 | $scope.currentPage = 1; | 55 | $scope.currentPage = 1; |
54 | $scope.selectPage(1); | 56 | $scope.selectPage(1); |
55 | }; | 57 | }; |
56 | 58 | ||
57 | $scope.selectPage = function (page) { | 59 | $scope.selectPage = function (page) { |
58 | console.info(page); | 60 | console.info(page); |
59 | var start = (page - 1) * $scope.numPerPage; | 61 | var start = (page - 1) * $scope.numPerPage; |
60 | var end = start + $scope.numPerPage; | 62 | var end = start + $scope.numPerPage; |
61 | $scope.paginas = []; | 63 | $scope.paginas = []; |
62 | $scope.paginas = calcularPages(page); | 64 | $scope.paginas = calcularPages(page); |
63 | $scope.currentPageProveedores = $scope.filteredProveedores.slice(start, end); | 65 | $scope.currentPageProveedores = $scope.filteredProveedores.slice(start, end); |
64 | $scope.currentPage = page; | 66 | $scope.currentPage = page; |
65 | }; | 67 | }; |
66 | 68 | ||
67 | $scope.select = function(proveedor) { | 69 | $scope.select = function(proveedor) { |
68 | $uibModalInstance.close(proveedor); | 70 | $uibModalInstance.close(proveedor); |
69 | }; | 71 | }; |
70 | 72 | ||
71 | $scope.cancel = function() { | 73 | $scope.cancel = function() { |
72 | $uibModalInstance.dismiss('cancel'); | 74 | $uibModalInstance.dismiss('cancel'); |
73 | }; | 75 | }; |
74 | 76 | ||
75 | $scope.busquedaDown = function(key) { | 77 | $scope.busquedaDown = function(key) { |
76 | if (key === 40) { | 78 | if (key === 40) { |
77 | primera(key); | 79 | primera(key); |
78 | } | 80 | } |
79 | }; | 81 | }; |
80 | 82 | ||
81 | $scope.itemProveedor = function(key) { | 83 | $scope.itemProveedor = function(key) { |
82 | if (key === 38) { | 84 | if (key === 38) { |
83 | anterior(key); | 85 | anterior(key); |
84 | } | 86 | } |
85 | 87 | ||
86 | if (key === 40) { | 88 | if (key === 40) { |
87 | siguiente(key); | 89 | siguiente(key); |
88 | } | 90 | } |
89 | 91 | ||
90 | if (key === 37) { | 92 | if (key === 37) { |
91 | retrocederPagina(); | 93 | retrocederPagina(); |
92 | } | 94 | } |
93 | 95 | ||
94 | if (key === 39) { | 96 | if (key === 39) { |
95 | avanzarPagina(); | 97 | avanzarPagina(); |
96 | } | 98 | } |
97 | }; | 99 | }; |
98 | 100 | ||
99 | function calcularPages(paginaActual) { | 101 | function calcularPages(paginaActual) { |
100 | var paginas = []; | 102 | var paginas = []; |
101 | paginas.push(paginaActual); | 103 | paginas.push(paginaActual); |
102 | 104 | ||
103 | if (paginaActual - 1 > 1) { | 105 | if (paginaActual - 1 > 1) { |
104 | 106 | ||
105 | paginas.unshift(paginaActual - 1); | 107 | paginas.unshift(paginaActual - 1); |
106 | if (paginaActual - 2 > 1) { | 108 | if (paginaActual - 2 > 1) { |
107 | paginas.unshift(paginaActual - 2); | 109 | paginas.unshift(paginaActual - 2); |
108 | } | 110 | } |
109 | } | 111 | } |
110 | 112 | ||
111 | if (paginaActual + 1 < $scope.lastPage) { | 113 | if (paginaActual + 1 < $scope.lastPage) { |
112 | paginas.push(paginaActual + 1); | 114 | paginas.push(paginaActual + 1); |
113 | if (paginaActual + 2 < $scope.lastPage) { | 115 | if (paginaActual + 2 < $scope.lastPage) { |
114 | paginas.push(paginaActual + 2); | 116 | paginas.push(paginaActual + 2); |
115 | } | 117 | } |
116 | } | 118 | } |
117 | 119 | ||
118 | if (paginaActual !== 1) { | 120 | if (paginaActual !== 1) { |
119 | paginas.unshift(1); | 121 | paginas.unshift(1); |
120 | } | 122 | } |
121 | 123 | ||
122 | if (paginaActual !== $scope.lastPage) { | 124 | if (paginaActual !== $scope.lastPage) { |
123 | paginas.push($scope.lastPage); | 125 | paginas.push($scope.lastPage); |
124 | } | 126 | } |
125 | 127 | ||
126 | return paginas; | 128 | return paginas; |
127 | } | 129 | } |
128 | 130 | ||
129 | function primera() { | 131 | function primera() { |
130 | $scope.selectedProveedores = 0; | 132 | $scope.selectedProveedores = 0; |
131 | } | 133 | } |
132 | 134 | ||
133 | function anterior() { | 135 | function anterior() { |
134 | if ($scope.selectedProveedores === 0 && $scope.currentPage > 1) { | 136 | if ($scope.selectedProveedores === 0 && $scope.currentPage > 1) { |
135 | retrocederPagina(); | 137 | retrocederPagina(); |
136 | } else { | 138 | } else { |
137 | $scope.selectedProveedores--; | 139 | $scope.selectedProveedores--; |
138 | } | 140 | } |
139 | } | 141 | } |
140 | 142 | ||
141 | function siguiente() { | 143 | function siguiente() { |
142 | if ($scope.selectedProveedores < $scope.currentPageProveedores.length - 1 ) { | 144 | if ($scope.selectedProveedores < $scope.currentPageProveedores.length - 1 ) { |
143 | $scope.selectedProveedores++; | 145 | $scope.selectedProveedores++; |
144 | } else { | 146 | } else { |
145 | avanzarPagina(); | 147 | avanzarPagina(); |
146 | } | 148 | } |
147 | } | 149 | } |
148 | 150 | ||
149 | function retrocederPagina() { | 151 | function retrocederPagina() { |
150 | if ($scope.currentPage > 1) { | 152 | if ($scope.currentPage > 1) { |
151 | $scope.selectPage($scope.currentPage - 1); | 153 | $scope.selectPage($scope.currentPage - 1); |
152 | $scope.selectedProveedores = $scope.numPerPage - 1; | 154 | $scope.selectedProveedores = $scope.numPerPage - 1; |
153 | } | 155 | } |
154 | } | 156 | } |
155 | 157 | ||
156 | function avanzarPagina() { | 158 | function avanzarPagina() { |
157 | if ($scope.currentPage < $scope.lastPage) { | 159 | if ($scope.currentPage < $scope.lastPage) { |
158 | $scope.selectPage($scope.currentPage + 1); | 160 | $scope.selectPage($scope.currentPage + 1); |
159 | $scope.selectedProveedores = 0; | 161 | $scope.selectedProveedores = 0; |
160 | } | 162 | } |
161 | } | 163 | } |
162 | }] | 164 | }] |
163 | ); | 165 | ); |
164 | 166 |