簡體   English   中英

如何修改它以具有類似Google的頁面調度?

[英]How can I modify this to have a Google-like paging?

我的控制器類包含

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頁,則結果為

在此處輸入圖片說明

如果我有100個Model.Pages會怎樣?

您需要確保分開,而不是返回模型中的所有數據-您最多僅返回頁面大小,並具有一個單獨的屬性來存儲總數。

例如:

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萬個結果並將“總計”設置為100萬個結果,但是只能在Items集合中插入10個結果。 您可以使用Linq將數據分頁到該集合中 我添加了為您執行此操作的方法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>
                    }

然后,您可以使用總計字段在頁面上顯示總數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM