File paging như này bị lỗi gì vậy ?

0 thích 0 không thích
1 lượt xem
đã hỏi 27 Tháng 7, 2022 bởi dinhtona (1,120 điểm)
@model TONA.Repositories.GenericClasses.PagedResultBase
@{
    int currentPage = Model.CurrentPage;
    int countPages = Model.PageCount;
    var generateUrl = Model.GenerateUrl;
    int? preview = null;
    int? next = null;

    if (currentPage > countPages)
        currentPage = countPages;

    if (countPages <= 1) return;

    if (currentPage > 1)
        preview = currentPage - 1;

    if (currentPage < countPages)
        next = currentPage + 1;

    List<int> pagesRanges = new List<int>();

    int delta = 5;
    int remain = delta * 2;

    pagesRanges.Add(currentPage);
    for (int i = 1; i <= delta; i++)
    {
        if (currentPage + i <= countPages)
        {
            pagesRanges.Add(currentPage + i);
            remain--;
        }

        if (currentPage - i >= 1)
        {
            pagesRanges.Insert(0, currentPage - i);
            remain--;
        }
    }

    if (remain > 0)
    {
        if (pagesRanges[0] == 1)
        {
            for (int i = 1; i <= remain; i++)
            {
                if (pagesRanges.Last() + 1 <= countPages)
                {
                    pagesRanges.Add(pagesRanges.Last() + 1);
                }
            }
        }
        else
        {
            for (int i = 1; i <= remain; i++)
            {
                if (pagesRanges.First() - 1 > 1)
                {
                    pagesRanges.Insert(0, pagesRanges.First() - 1);
                }
            }
        }
    }
}

<div class="d-flex">
    <ul class="pagination list-inline mx-auto justify-content-center">
        <!-- Previous page link -->
        @if (preview != null)
        {
            <li class="page-item">
                <a class="page-link" href="@generateUrl(1)">&#171;</a>
            </li>
            <li class="page-item">
                <a class="page-link" href="@generateUrl(preview)">&#8249;</a>
            </li>
        }
        else
        {
            <li class="page-item disabled">
                <a class="page-link" href="#" tabindex="-1" aria-disabled="true">&#171;</a>
            </li>
            <li class="page-item disabled">
                <a class="page-link" href="#" tabindex="-1" aria-disabled="true">&#8249;</a>
            </li>
        }

        <!-- Numbered page links -->
        @foreach (var pageitem in pagesRanges)
        {
            if (pageitem != currentPage)
            {
                <li class="page-item">
                    <a class="page-link" href="@generateUrl(pageitem)">
                        @pageitem
                    </a>
                </li>
            }
            else
            {
                <li class="page-item active" aria-current="page">
                    <a class="page-link" href="#">@pageitem <span class="sr-only">(current)</span></a>
                </li>
            }
        }

        <!-- Next page link -->
        @if (next != null)
        {
            <li class="page-item">
                <a class="page-link" href="@generateUrl(next)">&#8250;</a>
            </li>
            <li class="page-item">
                <a class="page-link" href="@generateUrl(countPages)">&#187;</a>
            </li>
        }
        else
        {
            <li class="page-item disabled">
                <a class="page-link" href="#" tabindex="-1" aria-disabled="true">&#8250;</a>
            </li>
            <li class="page-item disabled">
                <a class="page-link" href="#" tabindex="-1" aria-disabled="true">&#187;</a>
            </li>
        }
    </ul>
</div>
    

1 câu trả lời

0 thích 0 không thích
đã trả lời 28 Tháng 7, 2022 bởi nguyenthao (9,040 điểm)
Ra Tú dê trình bày nhé
...