Paging class, which I put in plugin/Paginate. js
Copy codeThe Code is as follows:
/**
* Paging plug-in class (the number of displays per page is missing, and listrows will be written tomorrow)
* @ Param page {Number} current page
* @ Param pagesize {Number} Number of records per page
* @ Param total {Number} total Number of 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;
}
}
/*
* Number of current start entries
*/
Paginate. prototype. first = function (){
Var first = (this. page-1) * this. pagesize;
If (first> this. total ){
Return (this. maxpage-1) * this. pagesize;
}
Return first;
}
/*
* Maximum 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;
Example
Copy codeThe Code is as follows:
Var Paginate = require ("../plugin/Paginate ");
Var q = req. query. q;
Var paginate = new Paginate (q, 10,185 );
Var page = paginate. page; // current page number
Var first = paginate. first (); // The current first
Var last = paginate. last (); // current maximum number of entries
Var maxpage = paginate. maxpage; // the total number of pages.
Var pagesize = paginate. pagesize; // number of entries displayed 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 })