[转载]【分享】一个Asp.net mvc下的分页控件MvcPagerX – 哥在博客园 – 博客园.
分页再平常不过的需求,其实也很简单完全可以写百行以内的代码扩展到HtmlHelper上实现,但是为了扩展性,我稍微(这所以是稍微,是因为本 人只稍微了解,还不敢说用得很熟练)用了点面向对象的思想对这个分页控件进行了设计,当然设计之前也参考了一些盆友的作品。吸取了一些思想的精华,下面就 讲讲怎么设计以及为什么要这么设计
接口部分:
using System.Collections.Generic; namespace MvcPagerx { /// <summary> /// 分页接口 /// </summary> public interface IPageAble { /// <summary> /// 一个按钮或者少于一个按钮的时候 /// </summary> /// <returns></returns> IList<PageButton> GetBtnLessThanOnePage(); /// <summary> /// 第一页时 /// </summary> /// <returns></returns> IList<PageButton> GetBtnWhenFrist(); /// <summary> /// 最后一页时 /// </summary> /// <returns></returns> IList<PageButton> GetBtnWhenLast(); /// <summary> /// 默认情况 /// </summary> /// <returns></returns> IList<PageButton> GetBtnWhenDefault(); /// <summary> /// 分页设置 /// </summary> PagerSettings PagerSetting { get; set; } } }
此接口主要标明实现类可以被分页,在需要分页的各种情况给出反馈,有
一个按钮或者少于一个按钮的时候
第一页时
最后一页时
默认情况
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Routing; using System.Web.Mvc; namespace MvcPagerx { /// <summary> /// 能够根据分页按钮生成HTML接口 /// </summary> public interface IGeneratePagerHtmlAble { /// <summary> /// PagerSetting /// </summary> PagerSettings PagerSetting { get; set; } /// <summary> /// Generate /// </summary> /// <param name="pageButtons"></param> /// <returns></returns> MvcHtmlString Generate(IList<PageButton> pageButtons); Func<RequestContext, string, int, string> UrlCallback { set; } } }
【分享】一个ASP.NET mvc下的分页控件MvcPagerX
2011-04-21 22:39 by 鲜宏, 1222 visits, 网摘, 收藏, 编辑
分页再平常不过的需求,其实也很简单完全可以写百行以内的代码扩展到HtmlHelper上实现,但是为了扩展性,我稍微(这所以是稍微,是因为本人只稍微了解,还不敢说用得很熟练)用了点面向对象的思想对这个分页控件进行了设计,当然设计之前也参考了一些盆友的作品。吸取了一些思想的精华,下面就讲讲怎么设计以及为什么要这么设计
接口部分:
using System.Collections.Generic;
namespace MvcPagerx
{
///
///
public interface IPageAble
{
///
///
///
IList
///
///
///
IList
///
///
///
IList
///
///
///
IList
///
///
PagerSettings PagerSetting { get; set; }
}
}
此接口主要标明实现类可以被分页,在需要分页的各种情况给出反馈,有
一个按钮或者少于一个按钮的时候
第一页时
最后一页时
默认情况
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Routing;
using System.Web.Mvc;
namespace MvcPagerx
{
///
///
public interface IGeneratePagerHtmlAble
{
///
///
PagerSettings PagerSetting { get; set; }
///
///
///
///
MvcHtmlString Generate(IList
Func
}
}
这个接口呢用于标明实现类具有生成HTML代码的功能,在这里我们可以看到,传入的参数是IList
实现类部分:
using System.Collections.Generic; using System.Linq; using System.Text; using System; namespace MvcPagerx { /// <summary> /// 普通的分页类 /// </summary> public class NormalPageProc : IPageAble { #region fields private int _StartPageIndex; private int _EndPageIndex; public PagerSettings PagerSetting { get; set; } #endregion #region Private Method private void Init() { _StartPageIndex = PagerSetting.CurrentPageIndex - (PagerSetting.NumericPagerCount / 2); if (_StartPageIndex + PagerSetting.NumericPagerCount > PagerSetting.PageCount) _StartPageIndex = PagerSetting.PageCount + 1 - PagerSetting.NumericPagerCount; if (_StartPageIndex < 1) _StartPageIndex = 1; _EndPageIndex = _StartPageIndex + PagerSetting.NumericPagerCount - 1; if (_EndPageIndex > PagerSetting.PageCount) _EndPageIndex = PagerSetting.PageCount; } private void AddFristButton(IList<PageButton> pageBtnList) { PageButton fristitem = new PageButton(PagerSetting.FirstPageText, 1, PagerSetting.CurrentPageIndex == 1, PageButtonType.FirstPageButton); pageBtnList.Add(fristitem); } private void AddPrevButton(IList<PageButton> pageBtnList) { var previtem = new PageButton(PagerSetting.PrevPageText, PagerSetting.CurrentPageIndex - 1, PagerSetting.CurrentPageIndex == 1, PageButtonType.PrevPageButton); pageBtnList.Add(previtem); } private void AddMoreButtonBefore(IList<PageButton> pageBtnList) { if (_StartPageIndex > 1 && PagerSetting.ShowMorePagerItems) { var index = _StartPageIndex - 1; if (index < 1) index = 1; PageButton item = new PageButton(PagerSetting.MorePageText, index, false, PageButtonType.MorePageButton); pageBtnList.Add(item); } } private void AddNumberButton(IList<PageButton> pageBtnList) { for (var pageIndex = _StartPageIndex; pageIndex <= _EndPageIndex; pageIndex++) { var text = pageIndex.ToString(); if (pageIndex == PagerSetting.CurrentPageIndex && !string.IsNullOrEmpty(PagerSetting.CurrentPageNumberFormatString)) text = String.Format(PagerSetting.CurrentPageNumberFormatString, text); else if (!string.IsNullOrEmpty(PagerSetting.PageNumberFormatString)) text = String.Format(PagerSetting.PageNumberFormatString, text); var item = new PageButton(text, pageIndex, false, PageButtonType.NumericPageButton); pageBtnList.Add(item); } } private void AddMoreButtonAfter(IList<PageButton> pageBtnList) { if (_EndPageIndex < PagerSetting.PageCount) { var index = _StartPageIndex + PagerSetting.NumericPagerCount; if (index > PagerSetting.PageCount) index = PagerSetting.PageCount; var item = new PageButton(PagerSetting.MorePageText, index, false, PageButtonType.MorePageButton); pageBtnList.Add(item); } } private void AddNextButton(IList<PageButton> pageBtnList) { var nextitem = new PageButton(PagerSetting.NextPageText, PagerSetting.CurrentPageIndex + 1, PagerSetting.CurrentPageIndex >= PagerSetting.PageCount, PageButtonType.NextPageButton); pageBtnList.Add(nextitem); } private void AddLastButton(IList<PageButton> pageBtnList) { var lastitem = new PageButton(PagerSetting.LastPageText, PagerSetting.PageCount, PagerSetting.CurrentPageIndex >= PagerSetting.PageCount, PageButtonType.LastPageButton); pageBtnList.Add(lastitem); } private IList<PageButton> AddButtons() { IList<PageButton> pageBtnList = new List<PageButton>(); AddFristButton(pageBtnList); //<---添加第一页 AddPrevButton(pageBtnList); //<---添加前一页 AddMoreButtonBefore(pageBtnList); //<---添加更多按钮(前置) AddNumberButton(pageBtnList); //<---添加数字分页按钮 AddMoreButtonAfter(pageBtnList); //<---添加更多按钮(后置) AddNextButton(pageBtnList); //<---添加下一页 AddLastButton(pageBtnList); //<---添加最后一页 IEnumerable<PageButton> currentPages = pageBtnList.Where(p => p.PageIndex == PagerSetting.CurrentPageIndex); foreach (PageButton btn in currentPages) btn.Disabled = true; return pageBtnList; } #endregion /// <summary> /// 一个按钮或者少于一个按钮的时候 /// </summary> /// <returns></returns> public IList<PageButton> GetBtnLessThanOnePage() { return new List<PageButton>() { new PageButton(PagerSetting.FirstPageText,1,true,PageButtonType.FirstPageButton) }; } /// <summary> /// 第一页时 /// </summary> /// <returns></returns> public IList<PageButton> GetBtnWhenFrist() { IList<PageButton> defaultPageButtons = GetBtnWhenDefault(); defaultPageButtons.SingleOrDefault(m => m.ButtonType == PageButtonType.PrevPageButton).Hide = true; defaultPageButtons.SingleOrDefault(m => m.ButtonType == PageButtonType.FirstPageButton).Hide = true; return defaultPageButtons; } /// <summary> /// 最后一页时 /// </summary> /// <returns></returns> public IList<PageButton> GetBtnWhenLast() { IList<PageButton> defaultPageButtons = GetBtnWhenDefault(); defaultPageButtons.SingleOrDefault(m => m.ButtonType == PageButtonType.NextPageButton).Hide = true; defaultPageButtons.SingleOrDefault(m => m.ButtonType == PageButtonType.LastPageButton).Hide = true; return defaultPageButtons; } /// <summary> /// 默认情况 /// </summary> /// <returns></returns> public IList<PageButton> GetBtnWhenDefault() { Init(); return AddButtons(); } } }
此类就是普通的分页生成器,在此类实现了IpageAble中定义的各种情况下需要生成的PageButton列表,然后返回给IGeneratePagerHtmlAble的实现类
如下的NormalPagerHtmlGeenerate
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Routing; using System.Web.Mvc; namespace MvcPagerx { /// <summary> /// 普通分页HTML代码生成类 /// </summary> internal class NormalPagerHtmlGenerate : IGeneratePagerHtmlAble { private Func<RequestContext, string, int, string> _GetUrlCallback; /// <summary> /// 分页设置 /// </summary> public PagerSettings PagerSetting { get; set; } private string WrapPageButton(PageButton btn) { string result = string.Empty; if (btn.Disabled) return String.Format("<li><a disabled=\"disabled\">{0}</a></li>", btn.Text); result = String.Format("<li><a href='{0}'>{1}</a></li>", _GetUrlCallback(PagerSetting.HtmlRequestContext, PagerSetting.PageParameterName, btn.PageIndex), btn.Text); return result; } /// <summary> /// 生成HTML代码 /// </summary> /// <param name="pageButtons">按钮列表</param> /// <returns></returns> public MvcHtmlString Generate(IList<PageButton> pageButtons) { TagBuilder tagBuilder = new TagBuilder(PagerSetting.TagName); tagBuilder.GenerateId(PagerSetting.TagID); if (!string.IsNullOrEmpty(PagerSetting.ClassName)) tagBuilder.AddCssClass(PagerSetting.ClassName); StringBuilder sb = new StringBuilder(); foreach (PageButton btn in pageButtons) { if (!btn.Hide) sb.Append(WrapPageButton(btn)); } tagBuilder.InnerHtml = sb.ToString(); return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.Normal)); } public Func<RequestContext, string, int, string> UrlCallback { set { _GetUrlCallback = value; } } } }
交给此类后便能生成HTML代码到前端展示了,还是相当的简单吧,更多的东西直接看代码吧,这里只是提一下大概思路。
MvcPagerx.rar