javascript pagination

1

I have a paging cod and I want it to do a count of items, ex: has 40 items and I want to read in 5 pages, then the first page reads 1 of 5, the second and 6 of 10 and the third and 11 of 15 and so on, my cod does that, only the last page is If you have an example, just two items, he keeps making the account as if he had 5, I'll pass the part of the code so he can help me:

$http({
method: 'GET',
url: '/user/distrito?page=' + $scope.page + '&size=' + size
}).then(function(response) {
$scope.distritos = response.data.content;
$scope.number = response.data.number;
$scope.totalDePaginas = response.data.totalPages;
$scope.dados2 = response.data.totalElements;
$scope.primeiraPagina = response.data.first;
$scope.ultimaPagina = response.data.last;
$scope.numeroElementos = response.data.numberOfElements;
$scope.size = response.data.size;

if (size > $scope.dados2) {
  $(".proximo").prop("disabled", true);
}

if ($scope.ultimaPagina == true) {
  $(".proximo").prop("disabled", true);
  $(".anterior").prop("disabled", false);
} else {
  $(".anterior").prop("disabled", false);
}

/* if($scope.ultimaPagina == false){
    $("#proximo").prop("disabled",false);
 }*/
$('.numeracaoUni').text(($scope.number + 1));

* * $('.numeracao').text(' ' + (($scope.size) * $scope.number + 1) + ' - ' + $scope.numeroElementos * ($scope.number + 1)); * *

$('.registros').text(' de  ' + $scope.totalDePaginas + ' ');

});

In bold and the part that does this, in the case he does the normal account until the last page because he is multiplying by the size only q is not what I want to have to give the right result at the end. Anyone who can help me, I appreciate it.

    
asked by anonymous 28.12.2017 / 16:04

1 answer

0

Old, it's hard to understand your code, but I believe that if you apply this code that I did in your project you can solve it easily, because I used the Array of numbers, you just change the array you want. p>

What I did was to give for to page passed in function GetItems(page) where i always starts from (pagina - 1) * itens por página , and i goes to pagina * itens por página with an additional condition that is the key to your problem is AND i menor que números de itens ie it does not exceed the maximum number, in this case by passing 4, it returns only the number 9.

var items = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
var numberOfItems = items.length;
var itemsPerPage = 3;

getItems(4);

function getItems(page) {
  var i;
  for (i = (page - 1) * itemsPerPage; i < page *itemsPerPage && i < numberOfItems; i++){
    console.log(items[i]);
  };

}

Any doubts are available.

    
28.12.2017 / 16:34