Commit 540ba79469c2ba9a16661cc2b1c13db7de249e4f
1 parent
332745ca6f
Exists in
master
buscador sin resultados hace focus
Showing
2 changed files
with
14 additions
and
6 deletions
Show diff stats
src/js/controller.js
1 | angular.module('focaModalVendedores') | 1 | angular.module('focaModalVendedores') |
2 | .controller('modalVendedoresCtrl', [ | 2 | .controller('modalVendedoresCtrl', [ |
3 | '$timeout', | ||
3 | '$filter', | 4 | '$filter', |
4 | '$scope', | 5 | '$scope', |
5 | '$uibModalInstance', | 6 | '$uibModalInstance', |
6 | 'focaVendedoresService', | 7 | 'focaVendedoresService', |
7 | function($filter, $scope, $uibModalInstance, focaVendedoresService) { | 8 | function($timeout, $filter, $scope, $uibModalInstance, focaVendedoresService) { |
8 | 9 | ||
9 | $scope.filters = ''; | 10 | $scope.filters = ''; |
10 | $scope.vendedores = []; | 11 | $scope.vendedores = []; |
11 | $scope.primerBusqueda = false; | 12 | $scope.primerBusqueda = false; |
12 | $scope.searchLoading = false; | 13 | $scope.searchLoading = false; |
13 | // pagination | 14 | // pagination |
14 | $scope.numPerPage = 10; | 15 | $scope.numPerPage = 10; |
15 | $scope.currentPage = 1; | 16 | $scope.currentPage = 1; |
16 | $scope.filteredVendedores = []; | 17 | $scope.filteredVendedores = []; |
17 | $scope.currentPageVendedores = []; | 18 | $scope.currentPageVendedores = []; |
18 | $scope.selectedVendedor = -1; | 19 | $scope.selectedVendedor = -1; |
19 | 20 | ||
20 | $scope.busquedaPress = function(key) { | 21 | $scope.busquedaPress = function(key) { |
21 | if (key === 13) { | 22 | if (key === 13) { |
22 | $scope.searchLoading = true; | 23 | $scope.searchLoading = true; |
23 | focaVendedoresService.getVendedores($scope.filters).then( | 24 | focaVendedoresService.getVendedores($scope.filters).then( |
24 | function(res) { | 25 | function(res) { |
25 | $scope.searchLoading = false; | 26 | $scope.searchLoading = false; |
26 | $scope.primerBusqueda = true; | 27 | $scope.primerBusqueda = true; |
27 | $scope.vendedores = res.data; | 28 | $scope.vendedores = res.data; |
28 | $scope.search(); | 29 | $scope.search(true); |
29 | primera(); | 30 | primera(); |
30 | } | 31 | } |
31 | ); | 32 | ); |
32 | } | 33 | } |
33 | }; | 34 | }; |
34 | 35 | ||
35 | $scope.search = function() { | 36 | $scope.search = function(pressed) { |
36 | if($scope.vendedores.length > 0) { | 37 | if($scope.vendedores.length > 0) { |
37 | $scope.filteredVendedores = $filter('filter')( | 38 | $scope.filteredVendedores = $filter('filter')( |
38 | $scope.vendedores, { $: $scope.filters } | 39 | $scope.vendedores, { $: $scope.filters } |
39 | ); | 40 | ); |
41 | |||
40 | $scope.lastPage = Math.ceil( | 42 | $scope.lastPage = Math.ceil( |
41 | $scope.filteredVendedores.length / $scope.numPerPage | 43 | $scope.filteredVendedores.length / $scope.numPerPage |
42 | ); | 44 | ); |
43 | $scope.resetPage(); | 45 | $scope.resetPage(); |
46 | }else if(pressed){ | ||
47 | $timeout(function() { | ||
48 | angular.element('#search')[0].focus(); | ||
49 | $scope.filters = ''; | ||
50 | }); | ||
44 | } | 51 | } |
45 | }; | 52 | }; |
46 | 53 | ||
47 | $scope.resetPage = function() { | 54 | $scope.resetPage = function() { |
48 | $scope.currentPage = 1; | 55 | $scope.currentPage = 1; |
49 | $scope.selectPage(1); | 56 | $scope.selectPage(1); |
50 | }; | 57 | }; |
51 | 58 | ||
52 | $scope.selectPage = function(page) { | 59 | $scope.selectPage = function(page) { |
53 | var start = (page - 1) * $scope.numPerPage; | 60 | var start = (page - 1) * $scope.numPerPage; |
54 | var end = start + $scope.numPerPage; | 61 | var end = start + $scope.numPerPage; |
55 | $scope.paginas = []; | 62 | $scope.paginas = []; |
56 | $scope.paginas = calcularPages(page); | 63 | $scope.paginas = calcularPages(page); |
57 | $scope.currentPageVendedores = $scope.filteredVendedores.slice(start, end); | 64 | $scope.currentPageVendedores = $scope.filteredVendedores.slice(start, end); |
58 | $scope.currentPage = page; | 65 | $scope.currentPage = page; |
59 | }; | 66 | }; |
60 | 67 | ||
61 | $scope.select = function(vendedor) { | 68 | $scope.select = function(vendedor) { |
62 | $uibModalInstance.close(vendedor); | 69 | $uibModalInstance.close(vendedor); |
63 | }; | 70 | }; |
64 | 71 | ||
65 | $scope.cancel = function() { | 72 | $scope.cancel = function() { |
66 | $uibModalInstance.dismiss('cancel'); | 73 | $uibModalInstance.dismiss('cancel'); |
67 | }; | 74 | }; |
68 | 75 | ||
69 | $scope.busquedaDown = function(key) { | 76 | $scope.busquedaDown = function(key) { |
70 | if (key === 40) { | 77 | if (key === 40) { |
71 | primera(key); | 78 | primera(key); |
72 | } | 79 | } |
73 | }; | 80 | }; |
74 | 81 | ||
75 | $scope.itemVendedor = function(key) { | 82 | $scope.itemVendedor = function(key) { |
76 | if (key === 38) { | 83 | if (key === 38) { |
77 | anterior(key); | 84 | anterior(key); |
78 | } | 85 | } |
79 | 86 | ||
80 | if (key === 40) { | 87 | if (key === 40) { |
81 | siguiente(key); | 88 | siguiente(key); |
82 | } | 89 | } |
83 | 90 | ||
84 | if (key === 37) { | 91 | if (key === 37) { |
85 | retrocederPagina(); | 92 | retrocederPagina(); |
86 | } | 93 | } |
87 | 94 | ||
88 | if (key === 39) { | 95 | if (key === 39) { |
89 | avanzarPagina(); | 96 | avanzarPagina(); |
90 | } | 97 | } |
91 | }; | 98 | }; |
92 | 99 | ||
93 | function calcularPages(paginaActual) { | 100 | function calcularPages(paginaActual) { |
94 | var paginas = []; | 101 | var paginas = []; |
95 | paginas.push(paginaActual); | 102 | paginas.push(paginaActual); |
96 | 103 | ||
97 | if (paginaActual - 1 > 1) { | 104 | if (paginaActual - 1 > 1) { |
98 | 105 | ||
99 | paginas.unshift(paginaActual - 1); | 106 | paginas.unshift(paginaActual - 1); |
100 | if (paginaActual - 2 > 1) { | 107 | if (paginaActual - 2 > 1) { |
101 | paginas.unshift(paginaActual - 2); | 108 | paginas.unshift(paginaActual - 2); |
102 | } | 109 | } |
103 | } | 110 | } |
104 | 111 | ||
105 | if (paginaActual + 1 < $scope.lastPage) { | 112 | if (paginaActual + 1 < $scope.lastPage) { |
106 | paginas.push(paginaActual + 1); | 113 | paginas.push(paginaActual + 1); |
107 | if (paginaActual + 2 < $scope.lastPage) { | 114 | if (paginaActual + 2 < $scope.lastPage) { |
108 | paginas.push(paginaActual + 2); | 115 | paginas.push(paginaActual + 2); |
109 | } | 116 | } |
110 | } | 117 | } |
111 | 118 | ||
112 | if (paginaActual !== 1) { | 119 | if (paginaActual !== 1) { |
113 | paginas.unshift(1); | 120 | paginas.unshift(1); |
114 | } | 121 | } |
115 | 122 | ||
116 | if (paginaActual !== $scope.lastPage) { | 123 | if (paginaActual !== $scope.lastPage) { |
117 | paginas.push($scope.lastPage); | 124 | paginas.push($scope.lastPage); |
118 | } | 125 | } |
119 | 126 | ||
120 | return paginas; | 127 | return paginas; |
121 | } | 128 | } |
122 | 129 | ||
123 | function primera() { | 130 | function primera() { |
124 | $scope.selectedVendedor = 0; | 131 | $scope.selectedVendedor = 0; |
125 | } | 132 | } |
126 | 133 | ||
127 | function anterior() { | 134 | function anterior() { |
128 | if ($scope.selectedVendedor === 0 && $scope.currentPage > 1) { | 135 | if ($scope.selectedVendedor === 0 && $scope.currentPage > 1) { |
129 | retrocederPagina(); | 136 | retrocederPagina(); |
130 | } else { | 137 | } else { |
131 | $scope.selectedVendedor--; | 138 | $scope.selectedVendedor--; |
132 | } | 139 | } |
133 | } | 140 | } |
134 | 141 | ||
135 | function siguiente() { | 142 | function siguiente() { |
136 | if ($scope.selectedVendedor < $scope.currentPageVendedores.length - 1) { | 143 | if ($scope.selectedVendedor < $scope.currentPageVendedores.length - 1) { |
137 | $scope.selectedVendedor++; | 144 | $scope.selectedVendedor++; |
138 | } else { | 145 | } else { |
139 | avanzarPagina(); | 146 | avanzarPagina(); |
140 | } | 147 | } |
141 | } | 148 | } |
142 | 149 | ||
143 | function retrocederPagina() { | 150 | function retrocederPagina() { |
144 | if ($scope.currentPage > 1) { | 151 | if ($scope.currentPage > 1) { |
145 | $scope.selectPage($scope.currentPage - 1); | 152 | $scope.selectPage($scope.currentPage - 1); |
146 | $scope.selectedVendedor = $scope.numPerPage - 1; | 153 | $scope.selectedVendedor = $scope.numPerPage - 1; |
147 | } | 154 | } |
148 | } | 155 | } |
149 | 156 | ||
150 | function avanzarPagina() { | 157 | function avanzarPagina() { |
151 | if ($scope.currentPage < $scope.lastPage) { | 158 | if ($scope.currentPage < $scope.lastPage) { |
152 | $scope.selectPage($scope.currentPage + 1); | 159 | $scope.selectPage($scope.currentPage + 1); |
153 | $scope.selectedVendedor = 0; | 160 | $scope.selectedVendedor = 0; |
154 | } | 161 | } |
155 | } | 162 | } |
156 | }] | 163 | }] |
157 | ); | 164 | ); |
158 | 165 |
src/views/modal-vendedores.html
1 | <div class="modal-header py-1"> | 1 | <div class="modal-header py-1"> |
2 | <div class="row w-100"> | 2 | <div class="row w-100"> |
3 | <div class="col-lg-6 col-xs-12"> | 3 | <div class="col-lg-6"> |
4 | <h5 class="modal-title my-1">Búsqueda de vendedores</h5> | 4 | <h5 class="modal-title my-1">Búsqueda de vendedores</h5> |
5 | </div> | 5 | </div> |
6 | <div class="input-group col-lg-6 col-xs-12 pr-0 my-2"> | 6 | <div class="input-group col-lg-6 pr-0 my-2"> |
7 | <input | 7 | <input |
8 | ladda="searchLoading" | 8 | ladda="searchLoading" |
9 | type="text" | 9 | type="text" |
10 | class="form-control form-control-sm" | 10 | class="form-control form-control-sm" |
11 | id="search" | ||
11 | placeholder="Busqueda" | 12 | placeholder="Busqueda" |
12 | ng-model="filters" | 13 | ng-model="filters" |
13 | ng-change="search()" | 14 | ng-change="search()" |
14 | ng-keydown="busquedaDown($event.keyCode)" | 15 | ng-keydown="busquedaDown($event.keyCode)" |
15 | ng-keypress="busquedaPress($event.keyCode)" | 16 | ng-keypress="busquedaPress($event.keyCode)" |
16 | foca-focus="selectedVendedor == -1" | 17 | foca-focus="selectedVendedor == -1" |
17 | ng-focus="selectedVendedor = -1" | 18 | ng-focus="selectedVendedor = -1" |
18 | teclado-virtual | 19 | teclado-virtual |
19 | > | 20 | > |
20 | <div class="input-group-append"> | 21 | <div class="input-group-append"> |
21 | <button | 22 | <button |
22 | ladda="searchLoading" | 23 | ladda="searchLoading" |
23 | class="btn btn-outline-secondary" | 24 | class="btn btn-outline-secondary" |
24 | type="button" | 25 | type="button" |
25 | ng-click="busquedaPress(13)" | 26 | ng-click="busquedaPress(13)" |
26 | > | 27 | > |
27 | <i class="fa fa-search" aria-hidden="true"></i> | 28 | <i class="fa fa-search" aria-hidden="true"></i> |
28 | </button> | 29 | </button> |
29 | </div> | 30 | </div> |
30 | </div> | 31 | </div> |
31 | </div> | 32 | </div> |
32 | </div> | 33 | </div> |
33 | <div class="modal-body" id="modal-body"> | 34 | <div class="modal-body" id="modal-body"> |
34 | 35 | ||
35 | <div ng-show="!primerBusqueda"> | 36 | <div ng-show="!primerBusqueda"> |
36 | Debe realizar una primer búsqueda. | 37 | Debe realizar una primer búsqueda. |
37 | </div> | 38 | </div> |
38 | 39 | ||
39 | <table ng-show="primerBusqueda" class="table table-striped table-sm col-12"> | 40 | <table ng-show="primerBusqueda" class="table table-striped table-sm col-12"> |
40 | <thead> | 41 | <thead> |
41 | <tr> | 42 | <tr> |
42 | <th>Código</th> | 43 | <th>Código</th> |
43 | <th>Nombre</th> | 44 | <th>Nombre</th> |
44 | <th></th> | 45 | <th></th> |
45 | </tr> | 46 | </tr> |
46 | </thead> | 47 | </thead> |
47 | <tbody> | 48 | <tbody> |
48 | <tr ng-show="currentPageVendedores.length == 0 && primerBusqueda"> | 49 | <tr ng-show="currentPageVendedores.length == 0 && primerBusqueda"> |
49 | <td colspan="3"> | 50 | <td colspan="3"> |
50 | No se encontraron resultados. | 51 | No se encontraron resultados. |
51 | </td> | 52 | </td> |
52 | </tr> | 53 | </tr> |
53 | <tr class="selected" | 54 | <tr class="selected" |
54 | ng-repeat="(key, vendedor) in currentPageVendedores" | 55 | ng-repeat="(key, vendedor) in currentPageVendedores" |
55 | ng-click="select(vendedor)" | 56 | ng-click="select(vendedor)" |
56 | > | 57 | > |
57 | <td ng-bind="vendedor.CodVen"></td> | 58 | <td ng-bind="vendedor.CodVen"></td> |
58 | <td ng-bind="vendedor.NomVen"></td> | 59 | <td ng-bind="vendedor.NomVen"></td> |
59 | <td class="d-md-none text-primary"> | 60 | <td class="d-md-none text-primary"> |
60 | <i class="fa fa-circle-thin" aria-hidden="true"></i> | 61 | <i class="fa fa-circle-thin" aria-hidden="true"></i> |
61 | </td> | 62 | </td> |
62 | <td class="d-none d-md-table-cell"> | 63 | <td class="d-none d-md-table-cell"> |
63 | <button | 64 | <button |
64 | type="button" | 65 | type="button" |
65 | class="btn btn-xs p-1 float-right" | 66 | class="btn btn-xs p-1 float-right" |
66 | ng-class="{ | 67 | ng-class="{ |
67 | 'btn-secondary': selectedVendedor != key, | 68 | 'btn-secondary': selectedVendedor != key, |
68 | 'btn-primary': selectedVendedor == key | 69 | 'btn-primary': selectedVendedor == key |
69 | }" | 70 | }" |
70 | foca-focus="selectedVendedor == {{key}}" | 71 | foca-focus="selectedVendedor == {{key}}" |
71 | ng-keydown="itemVendedor($event.keyCode)"> | 72 | ng-keydown="itemVendedor($event.keyCode)"> |
72 | <i class="fa fa-circle-thin" aria-hidden="true"></i> | 73 | <i class="fa fa-circle-thin" aria-hidden="true"></i> |
73 | </button> | 74 | </button> |
74 | </td> | 75 | </td> |
75 | </tr> | 76 | </tr> |
76 | </tbody> | 77 | </tbody> |
77 | </table> | 78 | </table> |
78 | </div> | 79 | </div> |
79 | <div class="modal-footer py-1"> | 80 | <div class="modal-footer py-1"> |
80 | <nav ng-show="currentPageVendedores.length > 0 && primerBusqueda" class="mr-auto"> | 81 | <nav ng-show="currentPageVendedores.length > 0 && primerBusqueda" class="mr-auto"> |
81 | <ul class="pagination pagination-sm justify-content mb-0"> | 82 | <ul class="pagination pagination-sm justify-content mb-0"> |
82 | <li class="page-item" ng-class="{'disabled': currentPage == 1}"> | 83 | <li class="page-item" ng-class="{'disabled': currentPage == 1}"> |
83 | <a class="page-link" href="javascript:void();" ng-click="selectPage(currentPage - 1)"> | 84 | <a class="page-link" href="javascript:void();" ng-click="selectPage(currentPage - 1)"> |
84 | <span aria-hidden="true">«</span> | 85 | <span aria-hidden="true">«</span> |
85 | <span class="sr-only">Anterior</span> | 86 | <span class="sr-only">Anterior</span> |
86 | </a> | 87 | </a> |
87 | </li> | 88 | </li> |
88 | <li | 89 | <li |
89 | class="page-item" | 90 | class="page-item" |
90 | ng-repeat="pagina in paginas" | 91 | ng-repeat="pagina in paginas" |
91 | ng-class="{'active': pagina == currentPage}" | 92 | ng-class="{'active': pagina == currentPage}" |
92 | > | 93 | > |
93 | <a | 94 | <a |
94 | class="page-link" | 95 | class="page-link" |
95 | href="javascript:void();" | 96 | href="javascript:void();" |
96 | ng-click="selectPage(pagina)" | 97 | ng-click="selectPage(pagina)" |
97 | ng-bind="pagina" | 98 | ng-bind="pagina" |
98 | ></a> | 99 | ></a> |
99 | </li> | 100 | </li> |
100 | <li class="page-item" ng-class="{'disabled': currentPage == lastPage}"> | 101 | <li class="page-item" ng-class="{'disabled': currentPage == lastPage}"> |
101 | <a class="page-link" href="javascript:void();" ng-click="selectPage(currentPage + 1)"> | 102 | <a class="page-link" href="javascript:void();" ng-click="selectPage(currentPage + 1)"> |
102 | <span aria-hidden="true">»</span> | 103 | <span aria-hidden="true">»</span> |
103 | <span class="sr-only">Siguiente</span> | 104 | <span class="sr-only">Siguiente</span> |
104 | </a> | 105 | </a> |
105 | </li> | 106 | </li> |
106 | </ul> | 107 | </ul> |
107 | </nav> | 108 | </nav> |
108 | <button class="btn btn-sm btn-secondary my-1" type="button" ng-click="cancel()">Cancelar</button> | 109 | <button class="btn btn-sm btn-secondary my-1" type="button" ng-click="cancel()">Cancelar</button> |
109 | </div> | 110 | </div> |
110 | 111 |