Lind.DDD.Paging分页模块介绍

回到目录

分页组件网上有很多,MVC.Pager,JSPager等,通过实现方式大体分为前端分页和后端分页,前端分页是前台对list内存本地集合进行分页,缺点就是在大数据情况下,内存占用过高;后端分页就是UI把要返回的页号告诉后台,由后台组织数据并返回,这种方法就是我们经常看到的了;而根据后台集合种类又可以分类List和IQueryable,前者是本地集合,在返回数据时,直接把第几页共几条的集合返回;IQueryable是预查询集合,它是Linq的产物,在很多地里它不通用,除非你的ORM框架支持Linq,否则建议用List本地集合,一般来说,分页的本地集合会在底层提供一些方法,它的参数一般包含了pageIndex和pageSize,下面可以看一下大叔针对mongodb制定的分页规范

      /// <summary>
/// MongoDB集成的查询方法,大数据情况下,有分页时使用这个方法
/// </summary>
/// <typeparam name="U">匿名对象,用来为条件赋值</typeparam>
/// <param name="template">条件对象</param>
/// <param name="limit"></param>
/// <param name="skip"></param>
/// <returns></returns>
PagedResult<TEntity> GetModel<U>(U template, int pageIndex, int pageSize);

而在Linq的底层,可以直接返回IQueryable集合,而在业务层进行返回Lind.DDD.Paging的分页集合即可,这块比较方便,今天主要说的是Linq下的分页方法

     /// <summary>
/// 拿到可查询结果集(Linq)
/// </summary>
/// <returns></returns>
System.Linq.IQueryable<TEntity> GetModel();

LINQ环境下大叔分页的介绍

分页基类

 public abstract class PageListBase<T> :
List<T>,
IEnumerable<T>,
IPagedList //繼承List<T>可以使用它的内部方法,如AddRange,Skip,Take等
{
/// <summary>
/// 初始化分页
/// </summary>
public PageListBase()
{
this.AddParameters = new NameValueCollection();
this.PageSize = ;
} /// <summary>
/// 总页数
/// </summary>
public int TotalPages { get; set; } /// <summary>
/// 总记录数
/// </summary>
public int TotalCount { get; set; } /// <summary>
/// 当前页
/// </summary>
public int PageIndex { get; set; } /// <summary>
/// 每页显示多少条记录
/// </summary>
public int PageSize { get; set; } /// <summary>
/// 是否有上一页
/// </summary>
public bool IsPreviousPage { get { return (PageIndex > ); } } /// <summary>
/// 是否有下一页
/// </summary>
public bool IsNextPage { get { return (PageIndex * PageSize) <= TotalCount; } } /// <summary>
/// 分页参数
/// </summary>
public NameValueCollection AddParameters { get; set; }
}

对于分页我们提供了传统分页和微博式(加载更多)分页,它们有自己的返回对象,看一下代码

    /// <summary>
/// 分页通用类(传统分页)
/// </summary>
/// <typeparam name="T"></typeparam>
public class PagedList<T> : PageListBase<T>
{
public PagedList()
: this(null, , )
{
}
/// <summary>
/// 数据源为IQueryable的范型
/// </summary>
/// <param name="source">数据源</param>
/// <param name="index">当前页</param>
/// <param name="pageSize">每页显示多少条记录</param>
public PagedList(IQueryable<T> source, int index, int pageSize)
{
if (source != null) //判断传过来的实体集是否为空
{
int total = source.Count();
this.TotalCount = total;
this.TotalPages = total / pageSize; if (total % pageSize > )
TotalPages++; this.PageSize = pageSize;
if (index > this.TotalPages)
{
index = this.TotalPages;
}
if (index < )
{
index = ;
}
this.PageIndex = index;
this.AddRange(source.Skip((index - ) * pageSize).Take(pageSize).ToList()); //Skip是跳到第几页,Take返回多少条
}
}
}

微博式分页主要是在原有数据基础上,进行追加数据,这在Linq里相当于一个Skip(0)加Take(PageIndex*PageSize)的动作,呵呵

    /// <summary>
/// 以追加的方式,显示内容,即从第1页开始显示N条记录
/// </summary>
/// <typeparam name="T"></typeparam>
public class PageListAppend<T> : PageListBase<T>
{
/// <summary>
/// 数据源为IQueryable的范型
/// </summary>
/// <param name="source">数据源</param>
/// <param name="index">当前页</param>
/// <param name="pageSize">每页显示多少条记录</param>
public PageListAppend(IQueryable<T> source, int index, int pageSize)
{
if (source != null) //判断传过来的实体集是否为空
{
int total = source.Count();
this.TotalCount = total;
this.TotalPages = total / pageSize; if (total % pageSize > )
TotalPages++; this.PageSize = pageSize;
if (index > this.TotalPages)
{
index = this.TotalPages;
}
if (index < )
{
index = ;
}
this.PageIndex = index;
this.AddRange(source.Skip().Take(index * pageSize).ToList()); //Skip是跳到第几页,Take返回多少条
}
}
}

有一点要特别注意一下,在LINQ分页时,我们看到被传入的集合一定是IQueryable的,如果是本地IList集合,那就失去了分页的意义,它相当于把所有数据加载到内存,然后再分页了。

有了数据的返回,下面再来看一下前台页面的组织,我们也封装了一个PagerHelper的方法,当然你也可以在分部页上进行设计,这都是可以的。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAATsAAADwCAIAAADw5lRTAAAOcUlEQVR4nO2dPc7buBZAtZ9ptZTsQLv4Km9ARYAHd6kywCtUpFKABwzibgbBax0MomKKdE6awWzgTiGJ4q8k57MlXvscuPhCUSTl6Phe0j8sBAD0UOw9AAC4AowF0ATGAmgCYwE0cTNjq0LqLijtpCykKKRqI+V+4b7cfEipa38Gfnx58/bDL28/vHzdeyQPx52NFRGRuozctTP1f45oL1fVvPmQZvq6Hd9e3n74xTzef/nrTv18/Ww3/unjhzd//D17wt/v3mPs7dnN2JvzemPvwSbG/vbuR//3PSXB2DxYMLYqpCylKIZHOxYaOc3f0Zo93l3bVkMd51Yec8jwdI+unqoVldOgefRDcspL6VbU/LkhzdRMXbsZUvSKUoUxbGMnkT59jARep/Dth18+fhORv/74LVrzzfup/OVr0tjo6SISGPvt5e1vb96b3j9/ErmidxhZNtbccObmSxkb1oz+M1WnrFeNOBW15qOZPeaVLVw1pFTNmVGZIa15ftJYxv748sayN6jg1eyd+fYyymM79unj4LMYOb9+dmx/2xsbPz32z28vk3vm0OreYWTZWHOjt9VwX6aMDWv2rLkjr5hDjgHNkyTVixdOUzVfM6SrZgSRIUWvKHGZAfY81tb173fvvXJLIRMwxyUiL6B9+hhEtmiMTZxuBuDFWD8XWN87jFxn7HyMDWv23NhYqwWTWEZ7scP+T8TYexibGpKpbF/RTKGFY4JVaG56U8F2eIxsP768iS1WXWFscq1rwdiXr9f0DiOrje2kDOTs51q+sVbNnjXGttX8bC1GJ6U1afRTU/to68fYaOD66SHN1HTaTA8pekVzhRMxY6ekV+Tr5yHG2oXO6RE91hqbOF1E5oydsvfVvcPIsrEmhTN33rQoUkldTsaGNXtMHmimbeHaj1c+M4OzqznitemVJ2uc0ZqvHNJ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5wVjY3Vj3Bg8CrxuOh7vcCUhWzePx6FY0N7x930crps/2O3Jrjr27MfJ4usi5ORybJhU5m7Mbah1j7bQYY8Fhb2Pj0zrbYrumsStyQ9sCmNPMYevGtyua4lRh2FGsH0crb+IqQR4Ri6cYC+vYfR7rr8q45a5oU1kYd6PrNVFPoxE+VhjvyJumJs+2CBefMBZ+kr1j7EQyRnqZamrKFzF2WTlzVqww3tH1xvoFS8b66TLGwkQ2xnozzUi2aYittcaM9c4eT4vm5KnCsKOYsVZuf24iiYPbyOx7sn1T5/61xn+WMBayMdbOlZuTiTjhio5bdYq9gQeBKkO3sZPjhdHimLHWOI+nsxcu/UuNv+CYzrz3cp0SjIW8P0Hhqp281a8jlZPf+n1h19ZYfm9VjBQ7x/tjGAvZG3uIT+9ewVbG3gGMhbyNFbES01t9BAJjQTH8BoUyNv7/gtzgd54ANIGxAJrAWABNYCyAJjAWQBMYC6AJjAXQBMYCaAJjATSBsQCawFgATWAsgCYwFkATGAugCYwF0ATGAmgCYwE0gbEAmsBYAE1gLIAmMBZAExgLoIkFY7u6LIqiKOtum+EAwCxrYmxXl0XV3n0oALDIqqy4rTAWIAswFkATq4wlLQbIhJVrxf0KFAtQADtDVgygCYwF0ATGAmgCYwE0scLYri5ZcwLIgxWfUsRWgGzgmwAAmsBYAE1gLIAmMBZAExgLoAmMBdAExgJo4l/tpbW/vXhlaQAAAABJRU5ErkJggg==" alt="" />

我们在前台调用时为大家封装了ajax分页,传统分页和微博分页等,直接在view上调用即可,当然如果你在前台对样式要求比较严格,也可以自己设计分布页来实现

@{
//分页灵活视图,根据自己项目的需要,去重写它
Layout = null;
var route = Request.RequestContext.RouteData.Values;
var controller = route["controller"].ToString();
var action = route["action"].ToString();
var currentPage = int.Parse((route["page"] ?? Request.QueryString["page"] ?? "").ToString()); var pageCount = int.Parse((route["pageCount"] ?? Request.QueryString["pageCount"] ?? "").ToString());
if (currentPage < )
{
currentPage = ;
}
if (currentPage > pageCount)
{
currentPage = pageCount;
}
}
<div>
<span>@Html.ActionLink("首页", action, controller, new { page = })</span>
<span>@Html.ActionLink("上一页", "Index", new { page = currentPage - })</span>
<span>@Html.ActionLink("下一页", "Index", new { page = currentPage + })</span>
<span>@Html.ActionLink("尾页", "Index", new { page = pageCount })</span>
<span>@currentPage/@pageCount</span>
</div>

感谢各位的耐心阅读!

回到目录

上一篇:HTML5: 10 个*使用 CSS3 动画的网页


下一篇:C#实用技能篇