Skip to content

Instantly share code, notes, and snippets.

@khoand0000
Last active December 3, 2015 09:57
Show Gist options
  • Save khoand0000/b48017c0cb4ef286bd53 to your computer and use it in GitHub Desktop.
Save khoand0000/b48017c0cb4ef286bd53 to your computer and use it in GitHub Desktop.
pagination calculating
  • I want to show result (from-to) at current page. Example: each page having 50 results, will show: 1-50, 51-100, ..., 501-503 final page has not enough 50 results
var resultsPerPage = 50;
var countOfResults = 451; // count of total results  ==> max of page = 10
var countOfCurrentPage = 50; // results of current page, from 1 --> 50
var page = 1; // current page, page begin from 1, not 0
var from = Math.min(countOfResults, 1 + (page - 1) * resultsPerPage); // is `countOfResults` when from page > max of page
var to = (page - 1) * resultsPerPage + countOfCurrentPage;
if (to > countOfResults) to = countOfResults;
console.log(from + '-' + to);

// exmample: countOfResults = 451, resultsPerPage = 50 ==> max of page = 10
// page=1, countOfCurrentPage = 50 ==> 1-50
// page=10, countOfCurrentPage = 1 ==> 451-451
// page=11, countOfCurrentPage = 50 ==> 451-451 // `from` and `to` is `countOfResults`
  • Check current page has previous page (newer page) or next page (older page)
var resultsPerPage = 50;
var countOfResults = 451;
var maxOfPages = 1 + Math.floor((countOfResults - 1) / resultsPerPage); // actually don't need to using `Math.floor()` because all variables are integer, `/` will be `integer division` (apply for almost programming language), but not for angular. When I apply the formular without `Math.floor()` in html template (angular), it apply `float division` instead `integer division`, so the formular is not right anymore --> need to using `Math.floor()` if you want apply `integer division` in html angular's template
var page = 3; // current page
var hasPreviousPage = page > 1; // newer page
var hasNextPage = page < maxOfPages; // older page

// example: resultsPerPage = 50
// countOfResults = 0 ==> maxOfPages = 1 + floor((0-1)/50) = 0
// countOfResults = 1 ==> maxOfPages = 1 + floor((1-1)/50) = 1
// countOfResults = 49 ==> maxOfPages = 1 + floor((49-1)/50) = 1
// countOfResults = 50 ==> maxOfPages = 1 + floor((50-1)/50) = 1 
// countOfResults = 51 ==> maxOfPages = 1 + floor((51-1)/50) = 2 
// countOfResults = 450 ==> maxOfPages = 1 + floor((450-1)/50) = 9 
// countOfResults = 451 ==> maxOfPages = 1 + floor((450-1)/50) = 10 

Why using Math.floor()? actually don't need to using Math.floor() because all variables are integer, / will be integer division (apply for almost programming language), but not for angular. When I apply the formular without Math.floor() in html template (angular), it apply float division instead integer division, so the formular is not right anymore --> need to using Math.floor() if you want apply integer division in html angular's template

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment