技术员联盟提供win764位系统下载,win10,win7,xp,装机纯净版,64位旗舰版,绿色软件,免费软件下载基地!

当前位置:主页 > 教程 > 服务器类 >

nodejs个人博客数据分页开发教程

来源:技术员联盟┆发布时间:2017-11-09 12:41┆点击:

  */

  var router=express.Router();

  /*每页条数*/

  var pageSize=4;

  /*首页*/

  router.get('http://www.3lian.com/',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);

  });

  分类列表分页路由::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

  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);

  });

  });

  });

  });

  }

  };

  模板部分

  href="/category//index/" rel="external nofollow" >上一页

  href="/category//index/" rel="external nofollow" >下一页

  效果图:

nodejs个人博客数据分页开发教程 三联

nodejs个人博客数据分页开发教程