利用ASP.NET MVC+Bootstrap搭建个人博客之打造清新分页Helper(三)
程序员文章站
2023-12-14 08:28:22
0. 没有找到一款中意的分页插件,又不想使用现成的(丑到爆),所以自己动手造一个吧
先看下效果(其实也不咋滴...):
我的小站地址:我的bootstrap小站...
0. 没有找到一款中意的分页插件,又不想使用现成的(丑到爆),所以自己动手造一个吧
先看下效果(其实也不咋滴...):
我的小站地址:我的bootstrap小站;
ps:(问博客园:为什么老是删我的置顶随便?上一篇阅读量都快500了,也分分钟给我从首页删掉...真是无语了<博客园地址:> )。
有点另类,分页直接是在后台拼接好html,然后发送到前台的:
1. 分页容器:
<div class="pagination"> <ul> //************分页html********* </ul> </div>
2. pagerhelper:
namespace zyn.blog.webhelper { public class pagerhelper { /// <summary> /// 生成分页html数据 /// </summary> /// <param name="currentpage">当前页</param> /// <param name="pagesize">页容量</param> /// <param name="totalcount">数据总条数</param> /// <returns>pagerhtmlstring</returns> public static string generatepagerstring(int currentpage, int pagesize, int totalcount) { var redirecttourl = httpcontext.current.request.url.absolutepath; pagesize = pagesize <= 0 ? 4 : pagesize; //1.0 总页数 int totalpages = math.max((totalcount + pagesize - 1) / pagesize, 1); //2.0 分页条的容量 int pagebarsize = 6;//默认一个分页条显示6个页码。分页条容量 //3.0 分页条的个数 totalpages个页数按 pagebarsize = 6 可以 分为多少个分页条 int pagebarnum = (totalpages + pagebarsize - 1) / pagebarsize; // totalpages=13时,也就是把13个页码分成3个分页条 //判断当前页currentpage坐落在第几个分页条内 int position = (currentpage - 1) / pagebarsize; //根据分页条的序号,计算出该分页条的第一个页码start和最后一个页码end int start = position * pagebarsize + 1; //如果是最后一个分页条,则需判断它实际的页条容量;否则页条容量就是pagebarsize int curbarcapacity = pagebarsize; if (position == pagebarnum - 1) { curbarcapacity = totalpages - (pagebarnum - 1) * pagebarsize; } stringbuilder pagerhtmlstring = new stringbuilder(); stringbuilder endhtmlstring = new stringbuilder(); //处理首页 pagerhtmlstring.appendformat("<li id='start-page'><a href='{0}?pageindex={1}&pagesize={2}'>首页</a></li> ", redirecttourl, 1, pagesize); //处理上一页:如果当前页不是第一页,就加上上一页 if (currentpage > 1) { pagerhtmlstring.appendformat("<li class='prev-page'><a href='{0}?pageindex={1}&pagesize={2}'>上一页</a></li> ", redirecttourl, currentpage - 1, pagesize); } //假如起始位置start为1 7 13 for (int i = 0; i < curbarcapacity; i++) //curbarcapacity=3 i=0 1 2 { int j = start + i; //要显示的页码当量值 if (j == currentpage) { //对当前页的处理:class=active pagerhtmlstring.appendformat("<li class='active show-page'><span>{0}</span></li> ", currentpage); if (curbarcapacity == pagebarsize) { //不处理最后一个分页条页 if (currentpage == start + pagebarsize - 1) { if (currentpage + 1 < totalpages) { endhtmlstring.appendformat("<li class='show-page'><a href='{0}?pageindex={1}&pagesize={2}'>{3}</a></li>", redirecttourl, currentpage + 1, pagesize, currentpage + 1); } endhtmlstring.append("<li class='show-page'><span>...</span></li>"); endhtmlstring.appendformat("<li class='show-page'><a href='{0}?pageindex={1}&pagesize={2}'>{3}</a></li>", redirecttourl, totalpages, pagesize, totalpages); } } } else { pagerhtmlstring.appendformat("<li class='show-page'><a href='{0}?pageindex={1}&pagesize={2}'>{3}</a></li> ", redirecttourl, j, pagesize, j); } } //省略号的处理 pagerhtmlstring.append(endhtmlstring.tostring()); //处理下一页:如果当前页不是最后一页,则加上下一页。也即中间的所有分页条都显示下一页 if (currentpage != totalpages) { pagerhtmlstring.appendformat("<li class='next-page'><a href='{0}?pageindex={1}&pagesize={2}'>下一页</a></li> ", redirecttourl, currentpage + 1, pagesize); } //处理末页 pagerhtmlstring.appendformat("<li id='end-page'><a href='{0}?pageindex={1}&pagesize={2}'>末页</a></li> ", redirecttourl, totalpages, pagesize); pagerhtmlstring.appendformat("<li id='total-page'><span>共-{0}-页</span></li>", totalpages); pagerhtmlstring.append(" "); return pagerhtmlstring.tostring(); } } }
3 渲染出来的分页html是这样的:
<div class="pagination"> <ul> <li id="start-page"><a href="/home/wrapartlist/0?pageindex=1&pagesize=6">首页</a></li> <li class="active show-page"><span>1</span></li> <li class="show-page"><a href="/home/wrapartlist/0?pageindex=2&pagesize=6">2</a></li> <li class="show-page"><a href="/home/wrapartlist/0?pageindex=3&pagesize=6">3</a></li> <li class="show-page"><a href="/home/wrapartlist/0?pageindex=4&pagesize=6">4</a></li> <li class="show-page"><a href="/home/wrapartlist/0?pageindex=5&pagesize=6">5</a></li> <li class="next-page"><a href="/home/wrapartlist/0?pageindex=2&pagesize=6">下一页</a></li> <li id="end-page"><a href="/home/wrapartlist/0?pageindex=5&pagesize=6">末页</a></li> <li id="total-page"><span>共-5-页</span></li> </ul> </div>
4. css样式:
/* 分页样式pagination ====== */ .pagination { /* div pagination*/ margin: 0; padding: 20px; text-align: center; font-size: 12px; display: block; } .pagination ul { display: inline-block; *display: inline; *zoom: 1; margin-left: 0; margin-bottom: 0; padding: 0; } .pagination ul > li { display: inline; } .pagination ul > li > a, .pagination ul > li > span { margin: 0 2px; float: left; padding: 5px 12px; background-color: #ddd; color: #666; border-radius: 2px; opacity: .88; } .pagination ul > li > a:hover, .pagination ul > li > a:focus { background-color: #bce8f1; } .pagination ul > .active > a, .pagination ul > .active > span { background-color: #45b6f7; color: #fff; } .pagination ul > .active > a, .pagination ul > .active > span { cursor: default; } .pagination ul > li > span, .pagination ul > .disabled > span, .pagination ul > .disabled > a, .pagination ul > .disabled > a:hover, .pagination ul > .disabled > a:focus { color: #999999; background-color: transparent; cursor: default; } /* 自定义的分页pagination 结束====== */
以上所述是小编给大家介绍的利用asp.net mvc+bootstrap搭建个人博客之打造清新分页helper(三),希望对大家有所帮助