I put the paged class in plugin/Paginate.js
The code copy is as follows:
/**
* Pagination plugin class (missing display count per page, listrows will be written tomorrow)
* @param page {Number} Current page
* @param pagesize {Number} Number of records per page
* @param total {Number} Total records
* @constructor
*/
function Paginate(page, pagesize, total){
if(!page || page <1){
page = 1;
}
if(!pagesize || pagesize<1){
pagesize = 20;
}
if(!total || total <0){
total = 0;
}
this.pagesize = pagesize;
this.total = total;
if(this.total%this.pagesize ===0){
this.maxpage = parseInt(this.total/this.pagesize);
}else{
this.maxpage = parseInt(this.total /this.pagesize) + 1;
}
if(page>this.maxpage){
this.page = this.maxpage;
}else{
this.page = page;
}
}
/*
* The current number of entries
*/
Paginate.prototype.first = function(){
var first = (this.page-1)*this.pagesize;
if(first>this.total){
return (this.maxpage-1)*this.pagesize;
}
return first;
}
/*
* The largest number of entries on the current page
*/
Paginate.prototype.last = function(){
var last = this.first()+this.pagesize;
if(last>this.total){
return this.total;
}
return last;
}
/**
* Previous page
* @returns {number}
*/
Paginate.prototype.prev = function(){
if(this.page <= 1){
return false;
}
return this.page-1;
}
/**
* Next page
* @returns {*}
*/
Paginate.prototype.next = function(){
if(this.page >= this.maxpage){
return false;
}
return (parseInt(this.page)+1);
}
module.exports = Paginate;
Use Example
The code copy is as follows:
var Paginate = require("../plugin/Paginate");
var q = req.query.q;
var paginate = new Paginate(q, 10, 185);
var page = page.page;//Current page count
var first = paginate.first();//The current first item
var last = paginate.last();//The current maximum number of entries
var maxpage = paginate.maxpage;//Total number of pages
var pagesize = paginate.pagesize;//Number of display per page
var total = paginate.total;//Total number of records
var prev = paginate.prev();//Previous
var next = paginate.next();//Next
res.json({page:page, first:first,last:last,maxpage:maxpage,pagesize:pagesize, total:total,prev:prev,next:next})