如何将其修改为具有类似Google的分页?

时间:2015-09-23 16:22:26

标签: c# asp.net-mvc

我的控制器类包含

var Paged = new PaginatedList<Products>(SideBar, page ?? 0, pageSize);
if (Request.IsAjaxRequest()) 
{
     return PartialView("~/Views/Shared/_Grid.cshtml", Paged);
}
return View(Paged);

PaginatedList是

public class PaginatedList<T> : List<T>
{

    public int PageIndex { get; private set; }
    public int PageSize { get; private set; }
    public int TotalCount { get; private set; }
    public int TotalPages { get; private set; }

    public PaginatedList(IQueryable<T> source, int pageIndex, int pageSize)
    {
        PageIndex = pageIndex;
        PageSize = pageSize;
        TotalCount = source.Count();
        TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);

        this.AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));
    }

    public bool HasPreviousPage
    {
        get
        {
            return (PageIndex > 0);
        }
    }

    public bool HasNextPage
    {
        get
        {
            return (PageIndex + 1 < TotalPages);
        }
    }
}

我的观点是

<div class="pagination-container">
            <nav class="pagination">
                <ul>
                    @for (int i = 0; i < Model.TotalPages; i++)
                    {
                       <li><a href="@Url.Action("Index", "Home", new { page = i})" 
                          class="@(i == Model.PageIndex ? "current-page" : "")">@(i + 1)</a></li>
                    }
                </ul>
            </nav>

            <nav class="pagination-next-prev">
                <ul>
                    @if (Model.HasPreviousPage) {
                        <li><a href="@Url.Action("Index", "Home", new { page = (Model.PageIndex - 1) })" class="prev"></a></li>
                    }
                    @if (Model.HasNextPage) { 
                        <li><a href="@Url.Action("Index", "Home", new { page = (Model.PageIndex + 1) })" class="next"></a></li>
                    }
                </ul>
            </nav>
            <div>
                Page @(Model.PageIndex + 1) of @Model.TotalPages
            </div>
        </div>

上述视图的一个问题是,它创建的数字页面等于模型中的页面大小。如果模型有6页,则结果为

enter image description here

如果我有100个Model.Pages,会发生什么?

1 个答案:

答案 0 :(得分:0)

您需要确保将您分开并且不返回模型中的所有数据 - 您最多只返回页面大小,并且具有单独的属性来存储总计数。

例如:

public class PageResult<T> where T : class
{

    public PageResult(IEnumerable<T> items, int page = 1, int pageSize = 10)
    {
        this.Items = items;
        this.PageSize = pageSize;
        this.PageIndex = page;
        this.PaginateData(page);
    }

    public IEnumerable<T> Items { get; private set; }

    public int PageSize { get; private set; }

    public int PageIndex { get; private set; }

    public int Total { get; private set; }

    public int TotalPages
    {
        get
        {
            return Math.Max((int)Math.Ceiling((Total / (double)PageSize)), 1);
        }
    }

    private int _MaxPagesToDisplay = 10;

    public int MaxPagesToDisplay
    {
        get { return _MaxPagesToDisplay; }
        set { _MaxPagesToDisplay = value; }
    }

    public int PagesToDisplay
    {
        get
        {
            return Math.Min(this.MaxPagesToDisplay, TotalPages);
        }
    }

    public void PaginateData(int page)
    {
        if(this.Items == null) return;
        this.Total = this.Items.Count();
        this.Items = this.Items.Skip(this.PageSize * this.PageIndex - 1).Take(this.PageSize);
    }

}

这意味着您可以返回100万个结果,并将总数设置为1百万,但只将10插入到Items集合中。 https://eval.in/438391。我添加了PaginateData方法,它可以帮到你。

然后您可以更新您的观点:

@for (int i = 1; i <= Model.PagesToDisplay; i++)
                    {
                       <li><a href="@Url.Action("Index", "Home", new { page = i})" 
                          class="@(i == Model.PageIndex ? "current-page" : "")">@(i)</a></li>
                    }

然后,您可以使用“总计”字段显示页面上的总计数。