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();
}
}
}