本文為大家分享了nodejs個(gè)人博客開發(fā)的數(shù)據(jù)分頁,具體內(nèi)容如下
控制器路由定義
首頁路由:http://localhost:8888/
首頁分頁路由:http://localhost:8888/index/2
/*** 首頁控制器*/var router=express.Router();/*每頁條數(shù)*/var pageSize=4;/*首頁*/ router.get('/',function(req,res,next){ var cid=0; F.model("article").assignIndexData(cid,1,pageSize,res);});/*首頁分頁*/router.get('/index/:page',function(req,res,next){ var currentPage=parseInt(req.params.page); var cid=0; F.model("article").assignIndexData(cid,currentPage,pageSize,res);});分類列表分頁路由:http://localhost:8888/category/分類id/分頁
/*分類頁*/router.get('/category/:cid/:page',function(req,res,next){ var cid=req.params.cid; var currentPage=parseInt(req.params.page); F.model("article").assignIndexData(cid,currentPage,pageSize,res);});模型數(shù)據(jù)部分
控制器調(diào)用article模型的assignIndexData()方法,參數(shù):分類id,當(dāng)前頁,每頁條數(shù),響應(yīng)對(duì)象
調(diào)用category模型的getAllList()方法得到分類list,參數(shù):回調(diào)函數(shù)
調(diào)用article模型的getCount()方法得到總條數(shù),參數(shù):分類id,回調(diào)函數(shù)
調(diào)用article模型的getArticlePager()方法得到文章對(duì)象的數(shù)據(jù)list,參數(shù):分類id,當(dāng)前頁,每頁條數(shù),回調(diào)函數(shù)
對(duì)上一頁,下一頁進(jìn)行-1和+1,并進(jìn)行判斷,上一頁應(yīng)大于0,下一頁應(yīng)小于等于總頁數(shù)(總條數(shù)/每頁條數(shù) 向上取整)
把數(shù)據(jù)分配到模板上
/*** 文章模型文件*/module.exports={ /*獲取條數(shù)*/ getCount:function(categoryId,callback){ var condition=""; if(categoryId!=0){ condition="where category_id="+categoryId; } var sql="select count(*) num from article "+condition; db.query(sql,callback); }, /*獲取分頁數(shù)據(jù)*/ getArticlePager:function(categoryId,currentPage,pageSize,callback){ if(currentPage<=0||!currentPage) currentPage=1; var start=(currentPage-1)*pageSize; var end=pageSize; var condition=""; if(categoryId!=0){ condition="where category_id="+categoryId; } var sql="select * from article "+condition+" order by time desc limit "+start+","+end; db.query(sql,callback); }, /*歸檔*/ getArchives:function(callback){ db.query("select time from article order by time desc",callback); }, /*分配首頁數(shù)據(jù)*/ assignIndexData:function(cid,currentPage,pageSize,res){ var categoryModel=F.model("category"); var articleModel=this; // 分類數(shù)據(jù) categoryModel.getAllList(function(err,categoryList){ // 文章條數(shù) articleModel.getCount(cid,function(err,nums){ // 文章分頁 articleModel.getArticlePager(cid,currentPage,pageSize,function(err,articleList){ var nextPage=(currentPage+1)>=Math.ceil(nums[0].num/pageSize) ? Math.ceil(nums[0].num/pageSize) : currentPage+1; var prePage=(currentPage-1)<=0 ? 1 : currentPage-1; // 歸檔 articleModel.getArchives(function(err,allArticleTime){ var newArticleTime=[]; for(var i=0;i<allArticleTime.length;i++){ newArticleTime.push(F.phpDate("y年m月",allArticleTime[i].time)); } /*分配數(shù)據(jù)*/ var data={ categoryList:categoryList, articleList:articleList, cid:cid, nextPage:nextPage==0 ? 1 : nextPage, prePage:prePage, allArticleTime:newArticleTime, currentPage:currentPage }; /*渲染模板*/ res.render("home/index",data); }); }); }); }); }};模板部分
<nav> <ul class="pager"> <li><a class="btn <%if(currentPage==prePage){%>disabled<%}%>" href="/<%if(cid!=0){%>category/<%=cid%>/<%}else{%>index/<%}%><%=prePage%>" rel="external nofollow" >上一頁</a></li> <li><a class="btn <%if(currentPage==nextPage){%>disabled<%}%>" href="/<%if(cid!=0){%>category/<%=cid%>/<%}else{%>index/<%}%><%=nextPage%>" rel="external nofollow" >下一頁</a></li> </ul> </nav>效果圖:


以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注