Home  >  Article  >  Web Front-end  >  nodejs paging class code sharing_node.js

nodejs paging class code sharing_node.js

WBOY
WBOYOriginal
2016-05-16 16:44:071511browse

Pagination class, I put it in plugin/Paginate.js

Copy code The code is as follows:

/**
* Paging plug-in class (lack of display number per page, 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; 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 starting number
*/
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 items 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;



Usage examples


Copy code

The 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 item
var last = paginate.last();//The current maximum number of items
var maxpage = paginate.maxpage;//Total number of pages
var pagesize = paginate.pagesize;//Number displayed per page
var total = paginate.total;//Total number of records
var prev = paginate.prev();//Previous article
var next = paginate.next();//Next article
res.json({page:page, first:first,last:last,maxpage:maxpage,pagesize:pagesize, total:total,prev:prev,next:next} )


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn