using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
namespace OurQu.Helper.WebHelper
{
using System.Text.RegularExpressions;
using OurQu.Helper.Tools;
public class PageHelper
{
/// <summary>
/// 每页显示几条记录
/// </summary>
public int PageSize { get; set; }
/// <summary>
/// 当前页码
/// </summary>
public int CurrentPage { get; set; }
/// <summary>
/// 总记录数
/// </summary>
public int Total { get; set; }
/// <summary>
/// 总页码
/// </summary>
public int PageTotal { get; set; }
/// <summary>
/// 当前URL地址
/// </summary>
public string Url { get; set; }
/// <summary>
/// url参数
/// </summary>
public string Partem { get; set; }
public IEnumerable<object> Data { get; set; }
public PageHelper() { }
public PageHelper(int Count, int SelectPage, int? pageSize)
{
//判断当前的页码
Url = HttpContext.Current.Request.Url.ToString();
if (SelectPage != 1)
CurrentPage = SelectPage;
else
CurrentPage = 1;
//一共有多少条记录
Total = Count;
//每页显示的记录
PageSize = pageSize ?? 10;
var FontArea = Url.IndexOf("?");
Partem = FontArea != -1 ? Url.Substring(FontArea) : "";
//计算总页数
PageTotal = (PageSize + Total - 1) / PageSize;
}
/// <summary>
/// 判断页码
/// </summary>
/// <param name="page"></param>
/// <returns></returns>
public string SelectedNum(int page)
{
if (CurrentPage != page)
return "<a href='" + Url + page + "'>" + page.ToString() + "</a>";
else
return "<a href='#' class='pageulNow' >" + page.ToString() + "</a>";
}
public string FormatPager()
{
StringBuilder str = new StringBuilder();
string temp = Regex.Replace(Url, @"((&)((?:page))|((?:page)))(=)(\d)", "");
//处理参数的时候用了正则表达式匹配page=Num || &page=Num
if (Partem.Length > 0)
temp += "&page=";
else
temp += "?page=";
//这边做了个小处理。。实在没想出来更简单的
Url = Regex.Replace(temp, "(\\?)(&)", "?");
str.Append("<div id='pageContainer' <div class ='pageul'> ");
if (CurrentPage > 1)
{
str.Append(string.Format("<a href='{0}1'>首页</a>", Url));
str.Append(string.Format("<a href='{0}{1}'>上一页</a>", Url, (CurrentPage - 1).ObjToInt()));
}
///偏移的起始位置
int startPage = CurrentPage > 7 ? CurrentPage - 7 : 1;
///偏移的结束位置
int stopPage = CurrentPage < PageTotal - 7 ? CurrentPage + 7 : PageTotal;
if (PageTotal > 1)
{
for (int item = startPage; item < stopPage + 1; item++)
str.Append(SelectedNum(item));
}
if (CurrentPage < PageTotal)
{
//str.Append("<a href='" + Url + Convert.ToInt32(CurrentPage + 1) + "' >下一页</a>");
str.Append(string.Format("<a href='{0}'>下一页</a>", Url + (CurrentPage + 1).ObjToInt()));
str.Append(string.Format("<a href='{0}{1}'>尾页</a>", Url, PageTotal));
}
//str.Append("<a> " + CurrentPage + "/" + PageTotal + "</span>页</a>");
str.Append(string.Format("<a> {0}/{1}</span>页</a>", CurrentPage, PageTotal));
str.Append(string.Format("共 {0} 条记录", Total));
str.Append("</div></div>");
return str.ToString();
}
}
}