Adapted from http://my.oschina.net/wanglihui/blog/269907, thanks to the original author, I carried out a two-time package based on
Pagination class, I put in plugin/paginate.js
/** * Page plug-in class (missing the number of displays per page, ListRows write tomorrow) *@paramPage {Number} current pages *@parampagesize {Number} records per page *@paramTotal {Number} 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;Ifthis.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 number of bars currently starting */paginate.prototype.first = function () {var first = (This.page-1) *This.pagesize;if (first>This.total) {Return (This.maxpage-1) *This.pagesize; }return first; }/** the maximum number of bars on the current page */paginate.prototype.last = function () {var last =This.first () +This.pagesize;if (last>This.total) {ReturnThis.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) { Span class= "Hljs-keyword" >return FALSE;} return (parseint (this.page) +1);} Module.exports = paginate;
Examples of Use
var paginate =Require".. /plugin/paginate ");var q = req.query.q;var paginate =New Paginate (q,10,185); var page = paginate.page; //Current page var first = Paginate.first (); The current first var last = Paginate.last (); Current maximum number of bars var maxpage = paginate.maxpage; Total Pages var pagesize = paginate.pagesize; 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})
Nodejs Pagination Class