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