js分页代码

Home文章
 简介:分页经常复用所以抽离出来了

函数

/*
条数,页码,每页条数,显示几个页码
count是必须的
*/

var page = function(count,currentpage,pagenum,pagestep){
 var currentpage = currentpage||1;
 currentpage--;
 var pagestep = pagestep||3;
 var pagenum = pagenum ||6;
 var totalpage = Math.ceil(count/pagenum);
 var firstpage = currentpage==0?1:0;
 var lastpage = (currentpage==totalpage-1)?1:0;
 return {
 count:count,
 pagenum:pagenum,
 totalpage:totalpage,
 currentpage : currentpage,
 skip:currentpage*pagenum,
 start:currentpage-pagestep<0?0:currentpage-pagestep,
 end:currentpage+pagestep>totalpage?totalpage:currentpage+pagestep,
 firstpage:firstpage,
 lastpage:lastpage
 }
};

//调用
var page = F.page(count,currentpage);

req.db.articles.find(range,{content:0}).sort({c_time: -1}).skip(page.skip).limit(page.pagenum).toArray(function(err, result) {
 var send = Object.assign({title: '文章列表' ,articles: result,type:range.creator?'myself':'all'},page);
 res.render('article',send);
 });

 

 

 



评论
Login