Commit 6511b331bf7146e79dcf4cfbaf0ef00ba1896f4b
1 parent
bca8dbec93
Exists in
master
buscador sin resultados hace focus
Showing
2 changed files
with
13 additions
and
4 deletions
Show diff stats
src/js/controller.js
| 1 | angular.module('focaModalCobradores') | 1 | angular.module('focaModalCobradores') |
| 2 | .controller('focaModalCobradoresController', [ | 2 | .controller('focaModalCobradoresController', [ |
| 3 | '$timeout', | ||
| 3 | '$filter', | 4 | '$filter', |
| 4 | '$scope', | 5 | '$scope', |
| 5 | '$uibModalInstance', | 6 | '$uibModalInstance', |
| 6 | 'focaModalCobradoresService', | 7 | 'focaModalCobradoresService', |
| 7 | function($filter, $scope, $uibModalInstance, focaModalCobradoresService) { | 8 | function($timeout, $filter, $scope, $uibModalInstance, focaModalCobradoresService) { |
| 8 | 9 | ||
| 9 | $scope.filters = ''; | 10 | $scope.filters = ''; |
| 10 | $scope.cobradores = []; | 11 | $scope.cobradores = []; |
| 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.filteredCobradores = []; | 17 | $scope.filteredCobradores = []; |
| 17 | $scope.currentPageCobradores = []; | 18 | $scope.currentPageCobradores = []; |
| 18 | $scope.selectedCobrador = -1; | 19 | $scope.selectedCobrador = -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 | focaModalCobradoresService.getCobradores($scope.filters).then( | 24 | focaModalCobradoresService.getCobradores($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.cobradores = res.data; | 28 | $scope.cobradores = 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) { |
| 37 | |||
| 36 | if($scope.cobradores.length > 0) { | 38 | if($scope.cobradores.length > 0) { |
| 37 | $scope.filteredCobradores = $filter('filter')( | 39 | $scope.filteredCobradores = $filter('filter')( |
| 38 | $scope.cobradores, { $: $scope.filters } | 40 | $scope.cobradores, { $: $scope.filters } |
| 39 | ); | 41 | ); |
| 40 | $scope.lastPage = Math.ceil( | 42 | $scope.lastPage = Math.ceil( |
| 41 | $scope.filteredCobradores.length / $scope.numPerPage | 43 | $scope.filteredCobradores.length / $scope.numPerPage |
| 42 | ); | 44 | ); |
| 43 | $scope.resetPage(); | 45 | $scope.resetPage(); |
| 46 | if(pressed && $scope.filteredCobradores.length === 0){ | ||
| 47 | $timeout(function() { | ||
| 48 | angular.element('#search')[0].focus(); | ||
| 49 | $scope.filters = ''; | ||
| 50 | }); | ||
| 51 | } | ||
| 44 | } | 52 | } |
| 45 | }; | 53 | }; |
| 46 | 54 | ||
| 47 | $scope.resetPage = function() { | 55 | $scope.resetPage = function() { |
| 48 | $scope.currentPage = 1; | 56 | $scope.currentPage = 1; |
| 49 | $scope.selectPage(1); | 57 | $scope.selectPage(1); |
| 50 | }; | 58 | }; |
| 51 | 59 | ||
| 52 | $scope.selectPage = function(page) { | 60 | $scope.selectPage = function(page) { |
| 53 | var start = (page - 1) * $scope.numPerPage; | 61 | var start = (page - 1) * $scope.numPerPage; |
| 54 | var end = start + $scope.numPerPage; | 62 | var end = start + $scope.numPerPage; |
| 55 | $scope.paginas = []; | 63 | $scope.paginas = []; |
| 56 | $scope.paginas = calcularPages(page); | 64 | $scope.paginas = calcularPages(page); |
| 57 | $scope.currentPageCobradores = $scope.filteredCobradores.slice(start, end); | 65 | $scope.currentPageCobradores = $scope.filteredCobradores.slice(start, end); |
| 58 | $scope.currentPage = page; | 66 | $scope.currentPage = page; |
| 59 | }; | 67 | }; |
| 60 | 68 | ||
| 61 | $scope.select = function(vendedor) { | 69 | $scope.select = function(vendedor) { |
| 62 | $uibModalInstance.close(vendedor); | 70 | $uibModalInstance.close(vendedor); |
| 63 | }; | 71 | }; |
| 64 | 72 | ||
| 65 | $scope.cancel = function() { | 73 | $scope.cancel = function() { |
| 66 | $uibModalInstance.dismiss('cancel'); | 74 | $uibModalInstance.dismiss('cancel'); |
| 67 | }; | 75 | }; |
| 68 | 76 | ||
| 69 | $scope.busquedaDown = function(key) { | 77 | $scope.busquedaDown = function(key) { |
| 70 | if (key === 40) { | 78 | if (key === 40) { |
| 71 | primera(key); | 79 | primera(key); |
| 72 | } | 80 | } |
| 73 | }; | 81 | }; |
| 74 | 82 | ||
| 75 | $scope.itemCobrador = function(key) { | 83 | $scope.itemCobrador = function(key) { |
| 76 | if (key === 38) { | 84 | if (key === 38) { |
| 77 | anterior(key); | 85 | anterior(key); |
| 78 | } | 86 | } |
| 79 | 87 | ||
| 80 | if (key === 40) { | 88 | if (key === 40) { |
| 81 | siguiente(key); | 89 | siguiente(key); |
| 82 | } | 90 | } |
| 83 | 91 | ||
| 84 | if (key === 37) { | 92 | if (key === 37) { |
| 85 | retrocederPagina(); | 93 | retrocederPagina(); |
| 86 | } | 94 | } |
| 87 | 95 | ||
| 88 | if (key === 39) { | 96 | if (key === 39) { |
| 89 | avanzarPagina(); | 97 | avanzarPagina(); |
| 90 | } | 98 | } |
| 91 | }; | 99 | }; |
| 92 | 100 | ||
| 93 | function calcularPages(paginaActual) { | 101 | function calcularPages(paginaActual) { |
| 94 | var paginas = []; | 102 | var paginas = []; |
| 95 | paginas.push(paginaActual); | 103 | paginas.push(paginaActual); |
| 96 | 104 | ||
| 97 | if (paginaActual - 1 > 1) { | 105 | if (paginaActual - 1 > 1) { |
| 98 | 106 | ||
| 99 | paginas.unshift(paginaActual - 1); | 107 | paginas.unshift(paginaActual - 1); |
| 100 | if (paginaActual - 2 > 1) { | 108 | if (paginaActual - 2 > 1) { |
| 101 | paginas.unshift(paginaActual - 2); | 109 | paginas.unshift(paginaActual - 2); |
| 102 | } | 110 | } |
| 103 | } | 111 | } |
| 104 | 112 | ||
| 105 | if (paginaActual + 1 < $scope.lastPage) { | 113 | if (paginaActual + 1 < $scope.lastPage) { |
| 106 | paginas.push(paginaActual + 1); | 114 | paginas.push(paginaActual + 1); |
| 107 | if (paginaActual + 2 < $scope.lastPage) { | 115 | if (paginaActual + 2 < $scope.lastPage) { |
| 108 | paginas.push(paginaActual + 2); | 116 | paginas.push(paginaActual + 2); |
| 109 | } | 117 | } |
| 110 | } | 118 | } |
| 111 | 119 | ||
| 112 | if (paginaActual !== 1) { | 120 | if (paginaActual !== 1) { |
| 113 | paginas.unshift(1); | 121 | paginas.unshift(1); |
| 114 | } | 122 | } |
| 115 | 123 | ||
| 116 | if (paginaActual !== $scope.lastPage) { | 124 | if (paginaActual !== $scope.lastPage) { |
| 117 | paginas.push($scope.lastPage); | 125 | paginas.push($scope.lastPage); |
| 118 | } | 126 | } |
| 119 | 127 | ||
| 120 | return paginas; | 128 | return paginas; |
| 121 | } | 129 | } |
| 122 | 130 | ||
| 123 | function primera() { | 131 | function primera() { |
| 124 | $scope.selectedCobrador = 0; | 132 | $scope.selectedCobrador = 0; |
| 125 | } | 133 | } |
| 126 | 134 | ||
| 127 | function anterior() { | 135 | function anterior() { |
| 128 | if ($scope.selectedCobrador === 0 && $scope.currentPage > 1) { | 136 | if ($scope.selectedCobrador === 0 && $scope.currentPage > 1) { |
| 129 | retrocederPagina(); | 137 | retrocederPagina(); |
| 130 | } else { | 138 | } else { |
| 131 | $scope.selectedCobrador--; | 139 | $scope.selectedCobrador--; |
| 132 | } | 140 | } |
| 133 | } | 141 | } |
| 134 | 142 | ||
| 135 | function siguiente() { | 143 | function siguiente() { |
| 136 | if ($scope.selectedCobrador < $scope.currentPageCobradores.length - 1) { | 144 | if ($scope.selectedCobrador < $scope.currentPageCobradores.length - 1) { |
| 137 | $scope.selectedCobrador++; | 145 | $scope.selectedCobrador++; |
| 138 | } else { | 146 | } else { |
| 139 | avanzarPagina(); | 147 | avanzarPagina(); |
| 140 | } | 148 | } |
| 141 | } | 149 | } |
| 142 | 150 | ||
| 143 | function retrocederPagina() { | 151 | function retrocederPagina() { |
| 144 | if ($scope.currentPage > 1) { | 152 | if ($scope.currentPage > 1) { |
| 145 | $scope.selectPage($scope.currentPage - 1); | 153 | $scope.selectPage($scope.currentPage - 1); |
| 146 | $scope.selectedCobrador = $scope.numPerPage - 1; | 154 | $scope.selectedCobrador = $scope.numPerPage - 1; |
| 147 | } | 155 | } |
| 148 | } | 156 | } |
| 149 | 157 | ||
| 150 | function avanzarPagina() { | 158 | function avanzarPagina() { |
| 151 | if ($scope.currentPage < $scope.lastPage) { | 159 | if ($scope.currentPage < $scope.lastPage) { |
| 152 | $scope.selectPage($scope.currentPage + 1); | 160 | $scope.selectPage($scope.currentPage + 1); |
| 153 | $scope.selectedCobrador = 0; | 161 | $scope.selectedCobrador = 0; |
| 154 | } | 162 | } |
| 155 | } | 163 | } |
| 156 | }] | 164 | }] |
| 157 | ); | 165 | ); |
| 158 | 166 |
src/views/modal-cobradores.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"> | 3 | <div class="col-lg-6"> |
| 4 | <h5 class="modal-title my-1">Búsqueda de cobradores</h5> | 4 | <h5 class="modal-title my-1">Búsqueda de cobradores</h5> |
| 5 | </div> | 5 | </div> |
| 6 | <div class="input-group col-lg-6 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 | placeholder="Busqueda" | 11 | placeholder="Busqueda" |
| 12 | ng-model="filters" | 12 | ng-model="filters" |
| 13 | ng-change="search()" | 13 | ng-change="search()" |
| 14 | ng-keydown="busquedaDown($event.keyCode)" | 14 | ng-keydown="busquedaDown($event.keyCode)" |
| 15 | ng-keypress="busquedaPress($event.keyCode)" | 15 | ng-keypress="busquedaPress($event.keyCode)" |
| 16 | foca-focus="selectedCobrador == -1" | 16 | foca-focus="selectedCobrador == -1" |
| 17 | ng-focus="selectedCobrador = -1" | 17 | ng-focus="selectedCobrador = -1" |
| 18 | teclado-virtual | 18 | teclado-virtual |
| 19 | id="search" | ||
| 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="currentPageCobradores.length == 0 && primerBusqueda"> | 49 | <tr ng-show="currentPageCobradores.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, cobrador) in currentPageCobradores" | 55 | ng-repeat="(key, cobrador) in currentPageCobradores" |
| 55 | ng-click="select(cobrador)" | 56 | ng-click="select(cobrador)" |
| 56 | > | 57 | > |
| 57 | <td ng-bind="cobrador.id"></td> | 58 | <td ng-bind="cobrador.id"></td> |
| 58 | <td ng-bind="cobrador.nombre"></td> | 59 | <td ng-bind="cobrador.nombre"></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': selectedCobrador != key, | 68 | 'btn-secondary': selectedCobrador != key, |
| 68 | 'btn-primary': selectedCobrador == key | 69 | 'btn-primary': selectedCobrador == key |
| 69 | }" | 70 | }" |
| 70 | foca-focus="selectedCobrador == {{key}}" | 71 | foca-focus="selectedCobrador == {{key}}" |
| 71 | ng-keydown="itemCobrador($event.keyCode)"> | 72 | ng-keydown="itemCobrador($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"> | 80 | <div class="modal-footer py-1"> |
| 80 | <nav ng-show="currentPageCobradores.length > 0 && primerBusqueda" class="mr-auto"> | 81 | <nav ng-show="currentPageCobradores.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 |