[nodejs] nodejs开发个人博客(六)数据分页

前端之家收集整理的这篇文章主要介绍了[nodejs] nodejs开发个人博客(六)数据分页前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

控制器路由定义

首页路由:http://localhost:8888/

首页分页路由:http://localhost:8888/index/2

首页控制器 router= pageSize=首页( cid=).assignIndexData(cid,首页分页( currentPage=parseInt(req. cid=

分类列表分页路由:http://localhost:8888/category/分类id/分页

分类页'/category/:cid/:page', cid= currentPage="article"

 模型数据部分

控制器调用article模型的assignIndexData()方法,参数:分类id,当前页,每页条数,响应对象

调用category模型的getAllList()方法得到分类list,参数:回调函数

调用article模型的getCount()方法得到总条数,参数:分类id,回调函数

调用article模型的getArticlePager()方法得到文章对象的数据list,参数:分类id,当前页,每页条数,回调函数

上一页下一页进行-1和+1,并进行判断,上一页应大于0,下一页应小于等于总页数(总条数/每页条数 向上取整)

把数据分配到模板上

文章模型文件 =获取条数 condition=""(categoryId!=0="where category_id="+ sql="select count(*) num from article "+sql,callback); },获取分页数据(currentPage<=0||!currentPage) currentPage=1 start=(currentPage-1)* end= condition=""(categoryId!=0="where category_id="+ sql="select * from article "+condition+" order by time desc limit "+start+","+sql,"select time from article order by time desc"首页数据 categoryModel=F.model("category" articleModel=分类数据 categoryModel.getAllList(文章条数 articleModel.getCount(cid,文章分页 articleModel.getArticlePager(cid,articleList){ nextPage=(currentPage+1)>=Math.ceil(nums[0].num/pageSize) ? Math.ceil(nums[0].num/pageSize) : currentPage+1 prePage=(currentPage-1)<=0 ? 1 : currentPage-1 articleModel.getArchives( newArticleTime=( i=0;iPHPDate("y年m月" data===0 ? 1 </span><span style="color: #008000;"&gt;/*</span><span style="color: #008000;"&gt;渲染模板</span><span style="color: #008000;"&gt;*/</span><span style="color: #000000;"&gt; res.render(</span>"home/index"<span style="color: #000000;"&gt;,data); }); }); }); }); }

};

 模板部分

<403_207@"上一页 <下一页

效果图:

原文链接:https://www.f2er.com/nodejs/69294.html

猜你在找的Node.js相关文章