Home > Web Front-end > JS Tutorial > body text

nodejs分页类代码分享_node.js

WBOY
Release: 2016-05-16 16:44:07
Original
1465 people have browsed it

分页类,我放在 plugin/Paginate.js

复制代码 代码如下:

/**
 * 分页插件类(缺少每页的显示数,listrows明天写)
 * @param page {Number} 当前页
 * @param pagesize {Number} 每页记录数
 * @param total {Number} 总记录数
 * @constructor
 */
function Paginate(page, pagesize, total){
    if(!page || page         page = 1;
    }
    if(!pagesize || pagesize        pagesize = 20;
    }
    if(!total || total         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;
    }
}

/*
* 当前开始的条数
*/
Paginate.prototype.first = function(){
    var first = (this.page-1)*this.pagesize;
    if(first>this.total){
        return (this.maxpage-1)*this.pagesize;
    }
    return first;
}
/*
* 当前页最大的条数
*/
Paginate.prototype.last = function(){
    var last = this.first()+this.pagesize;
    if(last>this.total){
        return this.total;
    }
    return last;
}

/**
 * 上一页
 * @returns {number}
 */
Paginate.prototype.prev = function(){
    if(this.page         return false;
    }
    return this.page-1;
}

/**
 * 下一页
 * @returns {*}
 */
Paginate.prototype.next = function(){
    if(this.page >= this.maxpage){
        return false;
    }
    return (parseInt(this.page)+1);
}
module.exports = Paginate;

使用例子

复制代码 代码如下:

var Paginate = require("../plugin/Paginate");
var q = req.query.q;
var paginate = new Paginate(q, 10, 185);
var page = paginate.page;//当前页数
var first = paginate.first();//当前第一条
var last = paginate.last();//当前最大条数
var maxpage = paginate.maxpage;//总页数   
var pagesize = paginate.pagesize;//每页显示数
var total = paginate.total;//总记录数
var prev = paginate.prev();//上一条
var next = paginate.next();//下一条
res.json({page:page, first:first,last:last,maxpage:maxpage,pagesize:pagesize, total:total,prev:prev,next:next})
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!