1、数据处理
首先在动态js中,根据url参数获取数据库文档的数量,设置分页的大小,获取当前页面的数据,然后将文档数量pagecount,分页大小pagesize,以及当前页面currentpage传递到页面中。
2、处理分页效果
我采用的是JavaScript动态生成的,你也可以利用ejs支持函数的特性将其封装后生成html形式的分页。
首先,添加分页ul,在你的页面中需要显示的位置添加代码:
lt;ul class="pagination" id="pagination"gt; lt;/ulgt;
然后在script标签中插入处理分页的代码:
$(document).ready(function() { if($("#pagination")){ var pagecount = lt;%= locals.pagecount %gt;; var pagesize = lt;%= locals.pagesize %gt;; var currentpage = lt;%= locals.currentpage %gt;; var counts,pagehtml=""; if(pagecount%pagesize==0){ counts = parseInt(pagecount/pagesize); }else{ counts = parseInt(pagecount/pagesize)+1; } //只有一页内容 if(pagecountlt;=pagesize){pagehtml="";} //大于一页内容 if(pagecountgt;pagesize){ if(currentpagegt;1){ pagehtml+= 'lt;ligt;lt;a rel="external nofollow" href="/course/index/'+(currentpage-1)+'"gt;上一页lt;/agt;lt;/ligt;'; } for(var i=0;ilt;counts;i++){ if(igt;=(currentpage-3) ilt;(currentpage+3)){ if(i==currentpage-1){ pagehtml+= 'lt;li class="active"gt;lt;a rel="external nofollow" href="/course/index/'+(i+1)+'"gt;'+(i+1)+'lt;/agt;lt;/ligt;'; }else{ pagehtml+= 'lt;ligt;lt;a rel="external nofollow" href="/course/index/'+(i+1)+'"gt;'+(i+1)+'lt;/agt;lt;/ligt;'; } } } if(currentpagelt;counts){ pagehtml+= 'lt;ligt;lt;a rel="external nofollow" href="/course/index/'+(currentpage+1)+'"gt;下一页lt;/agt;lt;/ligt;'; } } $("#pagination").html(pagehtml); } });
注意:locals.pagecount,locals.pagesize,locals.currentpage分别是数据库数量,分页大小,当前分页,他们是从js中传递过来的,当然,你也可以直接修改它们为固定的数据测试下效果。
比如:
实际效果为:
这样一个简单的分页效果就出来了
以上所述是小编给大家介绍的NodeJS和BootStrap分页效果的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!