nodejs個人博客開發(fā)第六步 數(shù)據(jù)分頁
本文為大家分享了nodejs個人博客開發(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,當前頁,每頁條數(shù),響應(yīng)對象
調(diào)用category模型的getAllList()方法得到分類list,參數(shù):回調(diào)函數(shù)
調(diào)用article模型的getCount()方法得到總條數(shù),參數(shù):分類id,回調(diào)函數(shù)
調(diào)用article模型的getArticlePager()方法得到文章對象的數(shù)據(jù)list,參數(shù):分類id,當前頁,每頁條數(shù),回調(diào)函數(shù)
對上一頁,下一頁進行-1和+1,并進行判斷,上一頁應(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>
效果圖:


以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
- jsp頁面數(shù)據(jù)分頁模仿百度分頁效果(實例講解)
- JSP數(shù)據(jù)分頁導(dǎo)出下載顯示進度條樣式
- JS代碼實現(xiàn)table數(shù)據(jù)分頁效果
- jsp+servlet+javabean實現(xiàn)數(shù)據(jù)分頁方法完整實例
- 無JS,完全php面向過程數(shù)據(jù)分頁實現(xiàn)代碼
- JSP數(shù)據(jù)庫操數(shù)據(jù)分頁顯示
- jquery+json實現(xiàn)數(shù)據(jù)列表分頁示例代碼
- js前臺分頁顯示后端JAVA數(shù)據(jù)響應(yīng)
- JS實現(xiàn)table表格數(shù)據(jù)排序功能(可支持動態(tài)數(shù)據(jù)+分頁效果)
- js對象實現(xiàn)數(shù)據(jù)分頁效果
相關(guān)文章
基于uni-app和Node.js實現(xiàn)app更新功能
uniapp 打包 ios,android 之后,有時候緊急修復(fù)或修改 ui,還需要走應(yīng)用市場審核,往往審核時間就需要幾天,如果是有bug需要升級就會很著急,要在uni-app中實現(xiàn)app更新功能,并使用Node.js作為后端服務(wù)本文給出了詳細的思路和步驟,需要的朋友可以參考下2024-08-08
Postman xmysql不切換環(huán)境緩存數(shù)據(jù)到本地
這篇文章主要為大家介紹了Postman xmysql不切換環(huán)境緩存數(shù)據(jù)到本地示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02
Node.JS在命令行中檢查Chrome瀏覽器是否安裝并打開指定網(wǎng)址
這篇文章主要介紹了Node.JS在命令行中檢查Chrome瀏覽器是否安裝,并打開指定網(wǎng)址,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05

