控制器路由定義
首頁路由:http://localhost:8888/
首頁分頁路由:http://localhost:8888/index/2
/**
* 首頁控制器
*/
var router=express.Router();
/*每頁條數*/
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);
});
複制
模型資料部分
控制器調用article模型的assignIndexData()方法,參數:分類id,目前頁,每頁條數,響應對象
調用category模型的getAllList()方法得到分類list,參數:回調函數
調用article模型的getCount()方法得到總條數,參數:分類id,回調函數
調用article模型的getArticlePager()方法得到文章對象的資料list,參數:分類id,目前頁,每頁條數,回調函數
對上一頁,下一頁進行-1和+1,并進行判斷,上一頁應大于0,下一頁應小于等于總頁數(總條數/每頁條數 向上取整)
把資料配置設定到模闆上
/**
* 文章模型檔案
*/
module.exports={
/*擷取條數*/
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);
},
/*擷取分頁資料*/
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);
},
/*配置設定首頁資料*/
assignIndexData:function(cid,currentPage,pageSize,res){
var categoryModel=F.model("category");
var articleModel=this;
// 分類資料
categoryModel.getAllList(function(err,categoryList){
// 文章條數
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));
}
/*配置設定資料*/
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%>">上一頁</a></li>
<li><a class="btn <%if(currentPage==nextPage){%>disabled<%}%>"
href="/<%if(cid!=0){%>category/<%=cid%>/<%}else{%>index/<%}%><%=nextPage%>">下一頁</a></li>
</ul>
</nav>
複制
效果圖:
