Esempio n. 1
0
        public static MvcHtmlString Pager(this HtmlHelper html, int currentPage, int pageSize, int totalRows, Func<int, string> url, PagerSettings settings)
        {
            int blockSize = settings.PagesRangeSize;

            var pagesCount = (int)Math.Ceiling((double)totalRows / pageSize);
            var blocksCount = (int)Math.Ceiling((double)pagesCount / blockSize);
            var currentBlock = 0;

            for (var i = 0; i < blocksCount; i++)
            {
                var floor = i * blockSize + 1;
                var ceil = (i + 1) * blockSize;
                if ((currentPage < floor) || (currentPage > ceil)) continue;
                currentBlock = i;
                break;
            }

            var startPage = currentBlock * blockSize + 1; //first visible page
            var endPage = (startPage + blockSize - 1); //last visible page
            if (endPage > pagesCount)
            {
                endPage = pagesCount;
            }

            StringBuilder sb = new StringBuilder(1024);
            //sb.AppendFormat("<span class=\"text\">{0}:&nbsp;</span>", settings.Pages);

            //first page, prev page)
            if (currentPage > 1)
            {
                sb.Append(RenderLink(url(1), settings.FirstPage));
                sb.Append(RenderLink(url(currentPage - 1), settings.PrevPage));
            }

            //previous dots
            if (startPage > 1) //first page not visible
            {
                int prevPage = startPage - 1;
                sb.Append(RenderLink(url(prevPage), "..."));
            }
            //pages links
            for (int i = startPage; i <= endPage; i++)
            {
                //if (currentPage == i)
                //{
                //	sb.AppendFormat("<span class=\"pager-item\">{0}</span>", i);
                //}
                //else
                //{
                //	sb.Append(RenderLink(url(i), i.ToString()));
                //}
                sb.Append(RenderLink(url(i), i.ToString(), currentPage == i));
            }
            //final dots
            if (endPage < pagesCount) //last page not visible
            {
                int nextPage = endPage + 1;
                sb.Append(RenderLink(url(nextPage), "..."));
            }
            //next page, last page
            if (currentPage < pagesCount)
            {
                sb.Append(RenderLink(url(currentPage + 1), settings.NextPage));
                sb.Append(RenderLink(url(pagesCount), settings.LastPage));
            }

            return MvcHtmlString.Create("<ul class='pagination'>" + sb + "</ul>");
        }
Esempio n. 2
0
 public static MvcHtmlString Pager(this HtmlHelper html, IPageableViewModel model, PagerSettings settings)
 {
     return Pager(html, model.Page, model.PageSize, model.TotalRows, x => GetDefaultPageUrl(x), settings);
 }