Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pagination logic in calculating total of pages

I'm using a react component that need me to pass in below prop

<Pager 
      totalPages={10}
      currentPage={1}
/>

I can't figure out the calculation as in the api I have total_items, not totalPages. if I have 50 total_items, how can I produce 5 for the totalPages prop? says my limit is 10.

like image 739
Cecilia Chan Avatar asked Sep 08 '17 03:09

Cecilia Chan


1 Answers

Divide total_items by limit, and round the value up.

Math.ceil(total_items/limit);

50 items / 10 per page = 5 pages
55 items / 10 per page = 6 pages
like image 118
fubar Avatar answered Oct 15 '22 01:10

fubar