Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

….

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?

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading