javascript paging

Asked

Viewed 303 times

1

I have a pagination OD and I want him to do an item count, ex: has 40 items and I want to read in 5 per 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 it, only q the last page if you have example, only two items he continues to make the account as if he had with 5, I’ll pass the code part so you 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 case it does the normal account until the last page because it’s multiplying by the size only q not and that q want to have to give the right result at the end. Who can help me I thank.

  • It wasn’t very clear to me, but if I understand correctly, try to exchange $Scope.size for $Scope.data2 (which I understand is the total of elements that you actually want to display).

  • is I want it to add up as you change pages, according to your items.has 40 items and I want to read in 5 by 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

1 answer

0

Old, it is difficult to understand your code, but I believe that if you apply this code I made in your project you can solve it easily, because I used the Number Array, you just need to change the array you want.

What I did was give one for in relation to the page passed on function GetItems(page) where the i always begins from (pagina - 1) * itens por página, and i goes to the pagina * itens por página with an additional condition that is the key to your problem AND i menor que números de itens that is it does not pass more than the maximum number, in this case 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 doubt I’m at your disposal.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.