20/06/2011

CTY mới

 Mai đi làm ngày đầu tiên ở cty mới sau 3 năm cày cuốc freelance và mở cty chưa đủ tài ...

Lại làm thuê kiếm bia uống...

15/06/2011

Paging - pager helper class c#

//định nghĩa class chia trang thường dùng

 public class Pager

    {

        public Pager(

            int totalItems,

            int currentPage = 1,

            int pageSize = 10,

            int maxPages = 10)

        {

            // calculate total pages

            var totalPages = (int)Math.Ceiling((decimal)totalItems / (decimal)pageSize);


            // ensure current page isn't out of range

            if (currentPage < 1)

            {

                currentPage = 1;

            }

            else if (currentPage > totalPages)

            {

                currentPage = totalPages;

            }


            int startPage, endPage;

            if (totalPages <= maxPages)

            {

                // total pages less than max so show all pages

                startPage = 1;

                endPage = totalPages;

            }

            else

            {

                // total pages more than max so calculate start and end pages

                var maxPagesBeforeCurrentPage = (int)Math.Floor((decimal)maxPages / (decimal)2);

                var maxPagesAfterCurrentPage = (int)Math.Ceiling((decimal)maxPages / (decimal)2) - 1;

                if (currentPage <= maxPagesBeforeCurrentPage)

                {

                    // current page near the start

                    startPage = 1;

                    endPage = maxPages;

                }

                else if (currentPage + maxPagesAfterCurrentPage >= totalPages)

                {

                    // current page near the end

                    startPage = totalPages - maxPages + 1;

                    endPage = totalPages;

                }

                else

                {

                    // current page somewhere in the middle

                    startPage = currentPage - maxPagesBeforeCurrentPage;

                    endPage = currentPage + maxPagesAfterCurrentPage;

                }

            }


            // calculate start and end item indexes

            var startIndex = (currentPage - 1) * pageSize;

            var endIndex = Math.Min(startIndex + pageSize - 1, totalItems - 1);


            // create an array of pages that can be looped over

            var pages = Enumerable.Range(startPage, (endPage + 1) - startPage);


            // update object instance with all pager properties required by the view

            TotalItems = totalItems;

            CurrentPage = currentPage;

            PageSize = pageSize;

            TotalPages = totalPages;

            StartPage = startPage;

            EndPage = endPage;

            StartIndex = startIndex;

            EndIndex = endIndex;

            Pages = pages;

        }


        public int TotalItems { get; private set; }

        public int CurrentPage { get; private set; }

        public int PageSize { get; private set; }

        public int TotalPages { get; private set; }

        public int StartPage { get; private set; }

        public int EndPage { get; private set; }

        public int StartIndex { get; private set; }

        public int EndIndex { get; private set; }

        public IEnumerable<int> Pages { get; private set; }

    }

//cách dùng trong mvc razor view

//css boostrap mặc định

    if (pager != null && pager.Pages.Any())

    {

        <nav class="table-responsive">

            <ul class="pagination justify-content-center d-flex flex-wrap">

                @if (pager.CurrentPage > 1)

                {

                    <li class="page-item">

                        <a class="page-link" href="@Url.Action("Index", "Home")">Trang đầu</a>

                    </li>

                    <li class="page-item">

                        <a class="page-link" href="@Url.Action("Index", "Home")?p=@(pager.CurrentPage - 1)">Trước</a>

                    </li>

                }


                @foreach (var p in pager.Pages)

                {

                    <li class="page-item @(p == pager.CurrentPage ? "active" : "")">

                        <a class="page-link" href="@Url.Action("Index", "Home")?p=@p">@p</a>

                    </li>

                }


                @if (pager.CurrentPage < pager.TotalPages)

                {

                    <li class="page-item">

                        <a class="page-link" href="@Url.Action("Index", "Home")?p=@(pager.CurrentPage + 1)">Tiếp</a>

                    </li>

                    <li class="page-item">

                        <a class="page-link" href="@Url.Action("Index", "Home")?p=@(pager.TotalPages)">Trang cuối</a>

                    </li>

                }

            </ul>

        </nav>

    }