Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

156
Views
Showing number of elements during paginations

I'm trying to create a way to show the number of elements for each page, for example:

Page1: 1-5 elements of 327

Page2: 6-10 elements of 327

....

Now I have the opportunity to go to the nextPage, previousPage, lastPage, firstPage, and also the possibility to choose the number of element for every page(for example 5, 10, 20) (this.elementsForPage)

I have tried to create in this way:

if(action === "next"){
          this.firstElement += parameters.length
          this.lastElement  += parameters.length
        }
        if(action === "previous"){
          this.firstElement -= parameters.length
          this.lastElement  -= parameters.length
        }
        if(action === "last"){
          this.firstElement = this.totalElements - this.parameters.length
          this.lastElement = this.totalElements
        }
        if(action === "first"){
          this.firstElement = 1;
          this.lastElement = parameters.length 
        }

this works if I make next, previous and first. But it doesn't work If I go on the last page.

This is why If I go to the last page and it has only 2 results, for example, i'll have

firstElement = 327 - 2 = 325

lastElement = 327

But if I go on "previous" it counts:

firstElement = 325 - 5 = 320

lastElement = 327 - 5 = 322

and it is wrong because are showing 5 results not 2.

In your opinion how can I fix these problems?

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

This Function

function getCurrentPageItems(currentPage,perPage,total)
{
    currentPage = currentPage - 1;
    let elStart = currentPage*perPage +1;
    let elEnd = elStart+perPage - 1;
    elEnd = Math.min(Math.max(elEnd, 1), total);
    console.log(`Page ${(currentPage+1)}: ${elStart}-${elEnd} of ${total}`);
    return [elEnd,elEnd];
}
getCurrentPageItems(1,5,327);
getCurrentPageItems(2,5,327);
getCurrentPageItems(65,5,327);
getCurrentPageItems(66,5,327);
console.log(327/5);

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!