/// <summary>
/// 分页控件
/// </summary>
/// <param name="htmlHeper">源HtmlHelper</param>
/// <param name="currentIndex">当前页(1-n)</param>
/// <param name="pageSize">页大小</param>
/// <param name="totalCount">总数</param>
/// <returns>Pager Html</returns>
public static MvcHtmlString LSPager(this HtmlHelper htmlHeper, int currentIndex, int pageSize, long totalCount, string styleClassStr = "shy_pagelist clearfix mar_t20 mar_b20")
{
#region 源html
//PS:由于样式决定,分页是倒序排列
/*
<div class="shy_pagelist clearfix mar_t20 mar_b20">
<a href="#">尾页</a>
<a href="#"><span class="icon iconfont"></span></a>
<a href="#">15</a>
<a href="javascript:void(0);">...</a>
<a href="#">7</a>
<a href="#">6</a>
<a href="#">5</a>
<a href="#">4</a>
<a href="#">3</a>
<a href="#">2</a>
<a href="#">1</a>
<a href="#"><span class="icon iconfont"></span></a>
<a href="#">首页</a>
</div>
*/
#endregion
//预计展示方式:首页 上一页 5 6 7 [8] 9 10 11 ... 14 下一页 尾页
//判断参数
if (totalCount <= 0)
{
return new MvcHtmlString("");
}
if (currentIndex <= 0)
{
currentIndex = 1;
}
if (pageSize <= 0)
{
pageSize = 1;
}
//计算总页数
long pageCount = totalCount / pageSize;
if (pageCount <= 0)
{
pageCount = 1;
}
else if (pageCount > 0 && (totalCount % pageSize) > 0)
{
pageCount += 1;
}
//处理总显示页数
long beginPage = 1;
long endPage = currentIndex <= 3 ? 7 : currentIndex + 3;
if (currentIndex >= 4 && pageCount > 8)
{
beginPage = currentIndex - 3;
}
if ((pageCount - currentIndex) <= 4 && pageCount > 8)
{
beginPage = pageCount - 7;
}
//获取当前地址
string query = htmlHeper.ViewContext.HttpContext.Request.Url.Query + "";
query = HttpUtility.UrlDecode(query);
string rawUrl = htmlHeper.ViewContext.HttpContext.Request.Url.ToString() + "";
rawUrl = HttpUtility.UrlDecode(rawUrl);
if (!string.IsNullOrWhiteSpace(query))
{
rawUrl = rawUrl.Replace(query, "");
}
rawUrl += "?pageindex={0}&pagesize={1}";
NameValueCollection collection = htmlHeper.ViewContext.HttpContext.Request.QueryString;
foreach (var item in collection.AllKeys)
{
if (item.ToLower() == "pageindex" || item.ToLower() == "pagesize")
{
continue;
}
rawUrl += string.Format("&{0}={1}", item, collection[item]);
}
StringBuilder sb = new StringBuilder();
sb.Append("<div class=\"" + styleClassStr + "\">");
//下一页和尾页
if (currentIndex < pageCount)
{
sb.AppendFormat("<a href=\"{0}\">尾页</a>", string.Format(rawUrl, pageCount, pageSize));
sb.AppendFormat("<a href=\"{0}\"><span class=\"tit16 jq_next_page\" style=\"font-family:'宋体';\">></span></a>", string.Format(rawUrl, currentIndex + 1, pageSize));
}
//绑定页数
for (long i = pageCount; i >= beginPage; i--)
{
//判定结束页
if (i > endPage && i == pageCount)
{
sb.AppendFormat("<a href=\"{1}\">{0}</a>", pageCount, string.Format(rawUrl, pageCount, pageSize));
continue;
}
if (i > endPage && i != pageCount)
{
sb.AppendFormat("<a href=\"javascript:void(0);\">...</a>");
i = endPage + 1;
continue;
}
//绑定选中页
if (i == currentIndex)
{
sb.AppendFormat("<a class=\"active\" href=\"javascript:void(0);\">{0}</a>", i);
}
else
{
sb.AppendFormat("<a href=\"{1}\">{0}</a>", i, string.Format(rawUrl, i, pageSize));
}
}
//首页和上一页
if (currentIndex > 1)
{
sb.AppendFormat("<a href=\"{0}\"><span class=\"tit16 jq_prev_page\" style=\"font-family:'宋体';\"><</span></a>", string.Format(rawUrl, currentIndex - 1, pageSize));
sb.AppendFormat("<a href=\"{0}\">首页</a>", string.Format(rawUrl, 1, pageSize));
}
sb.Append("</div>");
string html = sb.ToString();
sb.Clear();
// 一页不用显示
if (pageCount <= 1)
{
return new MvcHtmlString("");
}
return new MvcHtmlString(html);
}
//拼接当前的url
private static string GetCurrentUrl(int pageIndex,int pageSize)
{
string url = HttpContext.Current.Request.Url.ToString().ToLower();
string query = HttpContext.Current.Request.Url.Query.ToLower();
if (!string.IsNullOrWhiteSpace(query))
{
url = url.Replace(query, "");
}
url += "?pageindex="+pageIndex+"&pagesize="+pageSize;
//获取所有参数
NameValueCollection requestParams = HttpContext.Current.Request.QueryString;
foreach (var item in requestParams.AllKeys)
{
if (item.ToLower().Contains("pageindex") || item.ToLower().Contains("pagesize"))
{
continue;
}
url += item+"="+requestParams[item];
}
return url;
}