/// <summary>
/// 服务器端生成分页。
/// </summary>
public static class MotyPaginationHelper
{
/// <summary>
/// 生成分页HTML。
/// </summary>
/// <param name="helper"></param>
/// <param name="cur">当前页。</param>
/// <param name="total">总页数。</param>
/// <param name="option">分页选项。</param>
/// <returns></returns>
public static MvcHtmlString MotyPagination(this HtmlHelper helper, int cur, int total, MotyPaginationOption option)
{
if (cur < 0 || total < 0)
{
//throw new ArgumentException("Initialization Argument Error('cur' or 'total' must be greater than 0).");
return MvcHtmlString.Empty;
}
if (cur > total)
{
//throw new ArgumentException("Initialization Argument Error('cur' must be less than 'total').");
return MvcHtmlString.Empty;
}
string html = "";
if (!(total <= 1 && option.HideWhenOnlyOne))
{
html = CreateHtml(cur, total, option);
}
return MvcHtmlString.Create(html);
}
//获取当前需展示的页面按钮。
private static List<int> GetShownPages(int cur, int total, int pagesToShow)
{
var pages = new List<int>();
int half = (int)Math.Floor((pagesToShow - 1) / 2.0);
int pad = cur + half > total ? (cur + half - total) : 0;
int min = Math.Max(1, cur - half - pad);
for (var i = min; i <= cur; i++)
{
pages.Add(i);
}
var max = Math.Min(total, cur + pagesToShow - pages.Count);
for (var i = cur + 1; i <= max; i++)
{
pages.Add(i);
}
return pages;
}
//生成
private static string CreateHtml(int cur, int total, MotyPaginationOption option)
{
StringBuilder sb = new StringBuilder(string.Empty);
sb.Append("<div class='motypc_1604261319'>");
#region 首页
if (option.IsFirstShown)
{
if (cur == 1)
{
sb.AppendFormat("<span style='color:{0}'>{1}</span>",
ColorTranslator.ToHtml(option.DisabledForeColor), option.FirstText);
}
else
{
sb.AppendFormat("<a href='{0}' style='color:{1}' onmouseover=\"this.style.color='{2}';this.style.borderColor='{3}'\" onmouseout=\"this.style.color='{4}';this.style.borderColor='{5}'\">{6}</a>",
option.Url(1),
ColorTranslator.ToHtml(option.ForeColor), ColorTranslator.ToHtml(option.HoverForeColor),
ColorTranslator.ToHtml(option.HoverForeColor), ColorTranslator.ToHtml(option.ForeColor),
ColorTranslator.ToHtml(Color.Transparent), option.FirstText);
}
}
#endregion
#region 上一页
if (option.IsPrevShown)
{
if (cur == 1)
{
sb.AppendFormat("<span style='color:{0}'>{1}</span>",
ColorTranslator.ToHtml(option.DisabledForeColor), option.PrevText);
}
else
{
sb.AppendFormat("<a href='{0}' style='color:{1}' onmouseover=\"this.style.color='{2}';this.style.borderColor='{3}'\" onmouseout=\"this.style.color='{4}';this.style.borderColor='{5}'\">{6}</a>",
option.Url(cur - 1),
ColorTranslator.ToHtml(option.ForeColor), ColorTranslator.ToHtml(option.HoverForeColor),
ColorTranslator.ToHtml(option.HoverForeColor), ColorTranslator.ToHtml(option.ForeColor),
ColorTranslator.ToHtml(Color.Transparent), option.PrevText);
}
}
#endregion
#region 中间页面按钮
sb.Append("<div class='motypc_horizontal_pager_btn'>");
foreach (var page in GetShownPages(cur, total, option.PagesToShow))
{
if (page == cur)
{
sb.AppendFormat("<a href='javascript:void(0)' style='color:{0}; background-color:{1};border-color:transparent'>{2}</a>",
ColorTranslator.ToHtml(option.CurrentForeColor), ColorTranslator.ToHtml(option.CurrentBackColor), page);
}
else
{
sb.AppendFormat("<a href='{0}' style='color:{1}' onmouseover=\"this.style.color='{2}';this.style.borderColor='{3}'\" onmouseout=\"this.style.color='{4}';this.style.borderColor='{5}'\">{6}</a>",
option.Url(page),
ColorTranslator.ToHtml(option.ForeColor), ColorTranslator.ToHtml(option.HoverForeColor),
ColorTranslator.ToHtml(option.HoverForeColor), ColorTranslator.ToHtml(option.ForeColor),
ColorTranslator.ToHtml(Color.Transparent), page);
}
}
sb.Append("</div>");
sb.AppendFormat("<select class='motypc_vertical_pager_btn' onchange=\"window.location=this.options[this.selectedIndex].getAttribute('data-url')\">");
for (int i = 1; i <= total; i++)
{
if (i == cur)
{
sb.AppendFormat("<option selected='selected' value='{0}' data-url='{1}'>{2}</option>", i, option.Url(i), i);
}
else
{
sb.AppendFormat("<option value='{0}' data-url='{1}'>{2}</option>", i, option.Url(i), i);
}
}
sb.Append("</select>");
#endregion
#region 下一页
if (option.IsNextShown)
{
if (cur == total)
{
sb.AppendFormat("<span style='color:{0}'>{1}</span>",
ColorTranslator.ToHtml(option.DisabledForeColor), option.NextText);
}
else
{
sb.AppendFormat("<a href='{0}' style='color:{1}' onmouseover=\"this.style.color='{2}';this.style.borderColor='{3}'\" onmouseout=\"this.style.color='{4}';this.style.borderColor='{5}'\">{6}</a>",
option.Url(cur + 1),
ColorTranslator.ToHtml(option.ForeColor), ColorTranslator.ToHtml(option.HoverForeColor),
ColorTranslator.ToHtml(option.HoverForeColor), ColorTranslator.ToHtml(option.ForeColor),
ColorTranslator.ToHtml(Color.Transparent), option.NextText);
}
}
#endregion
#region 尾页
if (option.IsFirstShown)
{
if (cur == total)
{
sb.AppendFormat("<span style='color:{0}'>{1}</span>",
ColorTranslator.ToHtml(option.DisabledForeColor), option.LastText);
}
else
{
sb.AppendFormat("<a href='{0}' style='color:{1}' onmouseover=\"this.style.color='{2}';this.style.borderColor='{3}'\" onmouseout=\"this.style.color='{4}';this.style.borderColor='{5}'\">{6}</a>",
option.Url(total),
ColorTranslator.ToHtml(option.ForeColor), ColorTranslator.ToHtml(option.HoverForeColor),
ColorTranslator.ToHtml(option.HoverForeColor), ColorTranslator.ToHtml(option.ForeColor),
"transparent", option.LastText);
}
}
#endregion
sb.Append("</div>");
return sb.ToString();
}
}
/// <summary>
/// 分页选项。
/// </summary>
public class MotyPaginationOption
{
private Color foreColor = ColorTranslator.FromHtml("#787d82");
/// <summary>
/// 获取或设置正常文本颜色。
/// </summary>
public Color ForeColor
{
get { return foreColor; }
set
{
if (foreColor.Equals(value))
return;
foreColor = value;
}
}
private Color disabledForeColor = ColorTranslator.FromHtml("#abaaa9");
/// <summary>
/// 获取或设置失效文本颜色。
/// </summary>
public Color DisabledForeColor
{
get { return disabledForeColor; }
set
{
if (disabledForeColor.Equals(value))
return;
disabledForeColor = value;
}
}
private Color hoverForeColor = ColorTranslator.FromHtml("#ec1500");
/// <summary>
/// 获取或设置鼠标悬停文本颜色及该项的下边界颜色。
/// </summary>
public Color HoverForeColor
{
get { return hoverForeColor; }
set
{
if (hoverForeColor.Equals(value))
return;
hoverForeColor = value;
}
}
private Color currentForeColor = ColorTranslator.FromHtml("#fff");
/// <summary>
/// 获取或设置当前选中页按钮文字颜色。
/// </summary>
public Color CurrentForeColor
{
get { return currentForeColor; }
set
{
if (currentForeColor.Equals(value))
return;
currentForeColor = value;
}
}
private Color currentBackColor = ColorTranslator.FromHtml("#ec1500");
/// <summary>
/// 获取或设置当前选中页按钮背景颜色。
/// </summary>
public Color CurrentBackColor
{
get { return currentBackColor; }
set
{
if (currentBackColor.Equals(value))
return;
currentBackColor = value;
}
}
private int pagesToShow = 5;
/// <summary>
/// 获取或设置显示的页面按钮数目,不包括首页、尾页、上一页和下一页,建议设成奇数。
/// </summary>
public int PagesToShow
{
get { return pagesToShow; }
set
{
if (pagesToShow == value)
return;
pagesToShow = value;
}
}
private string firstText = "首页";
/// <summary>
/// 获取或设置首页按钮显示文本。
/// </summary>
public string FirstText
{
get { return firstText; }
set
{
if (firstText.Equals(value))
return;
firstText = value;
}
}
private string lastText = "尾页";
/// <summary>
/// 获取或设置尾页按钮显示文本。
/// </summary>
public string LastText
{
get { return lastText; }
set
{
if (lastText.Equals(value))
return;
lastText = value;
}
}
private string prevText = "上一页";
/// <summary>
/// 获取或设置上一页按钮显示文本。
/// </summary>
public string PrevText
{
get { return prevText; }
set
{
if (prevText.Equals(value))
return;
prevText = value;
}
}
private string nextText = "下一页";
/// <summary>
/// 获取或设置下一页按钮显示文本。
/// </summary>
public string NextText
{
get { return nextText; }
set
{
if (nextText.Equals(value))
return;
nextText = value;
}
}
private bool isFirstShown = true;
/// <summary>
/// 获取或设置是否显示首页按钮。
/// </summary>
public bool IsFirstShown
{
get { return isFirstShown; }
set
{
if (isFirstShown == value)
return;
isFirstShown = value;
}
}
private bool isLastShown = true;
/// <summary>
/// 获取或设置是否显示尾页按钮。
/// </summary>
public bool IsLastShown
{
get { return isLastShown; }
set
{
if (isLastShown == value)
return;
isLastShown = value;
}
}
private bool isPrevShown = true;
/// <summary>
/// 获取或设置是否显示上一页按钮。
/// </summary>
public bool IsPrevShown
{
get { return isPrevShown; }
set
{
if (isPrevShown == value)
return;
isPrevShown = value;
}
}
private bool isNextShown = true;
/// <summary>
/// 获取或设置显示下一页按钮。
/// </summary>
public bool IsNextShown
{
get { return isNextShown; }
set
{
if (isNextShown == value)
return;
isNextShown = value;
}
}
private bool isHorizontal = true;
/// <summary>
/// 获取或设置横向还是竖向,如果是横向,那么将是一字排开,否则将会是一个select。
/// </summary>
public bool IsHorizontal
{
get { return isHorizontal; }
set
{
if (isHorizontal == value)
return;
isHorizontal = value;
}
}
private bool hideWhenOnlyOne = true;
/// <summary>
/// 获取或设置当只有一项时是否隐藏。
/// </summary>
public bool HideWhenOnlyOne
{
get { return hideWhenOnlyOne; }
set
{
if (hideWhenOnlyOne == value)
return;
hideWhenOnlyOne = value;
}
}
private Func<int, string> url = (index) => { return index.ToString(); };
/// <summary>
/// 获取或设置页面跳转链接。
/// </summary>
public Func<int, string> Url
{
get { return url; }
set
{
if (url.Equals(value))
{
return;
}
url = value;
}
}
}
后台分页类实现
Java
416
0
0
2022-04-30
登录后可点赞和收藏
登录后可点赞和收藏