示例#1
0
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            if (PagingModel == null)
            {
                // allow for passing in the settings separately
                PagingModel                           = new PaginationSettings();
                PagingModel.CurrentPage               = PageNumber;
                PagingModel.ItemsPerPage              = PageSize;
                PagingModel.TotalItems                = TotalItems;
                PagingModel.MaxPagerItems             = MaxPagerItems;
                PagingModel.SuppressEmptyNextPrev     = SuppressEmptyNextPrev;
                PagingModel.SuppressInActiveFirstLast = SuppressInActiveFirstLast;
            }

            if (ShowFirstLast)
            {
                PagingModel.ShowFirstLast = true;
            }

            if (!ShowNumbered)
            {
                PagingModel.ShowNumbered = false;
            }

            if (UseReverseIncrement)
            {
                PagingModel.UseReverseIncrement = true;

                if (SuppressEmptyNextPrev)
                {
                    PagingModel.SuppressEmptyNextPrev = true;
                }
            }


            int totalPages = (int)Math.Ceiling(PagingModel.TotalItems / (double)PagingModel.ItemsPerPage);

            // don't render if only 1 page
            if (totalPages <= 1)
            {
                output.SuppressOutput();
                return;
            }

            //change the cs-pager element into a ul
            output.TagName = "ul";

            string querySeparator;

            //prepare things needed by generatpageeurl function
            TagBuilder linkTemplate = GenerateLinkTemplate();

            baseHref       = linkTemplate.Attributes["href"] ?? string.Empty;
            querySeparator = baseHref.Contains("?") ? "&" : "?";
            baseHref       = baseHref + querySeparator + PageNumberParam + "=";

            List <PaginationLink> links = linkBuilder.BuildPaginationLinks(
                PagingModel,
                GeneratePageUrl,
                FirstPageText,
                FirstPageTitle,
                PreviousPageText,
                PreviousPageTitle,
                NextPageText,
                NextPageTitle,
                LastPageText,
                LastPageTitle,
                "...");

            foreach (PaginationLink link in links)
            {
                var li = new TagBuilder("li");

                if (link.IsCurrent)
                {
                    li.AddCssClass(LiCurrentCssClass);
                }

                if (!link.Active)
                {
                    li.AddCssClass(LiNonActiveCssClass);
                }

                var a = new TagBuilder("a");

                if (link.Active && (link.Url.Length > 0))
                {
                    a.MergeAttribute("href", link.Url);
                }
                else
                {
                    a.MergeAttribute("href", "#");
                }


                if (link.Text == "«")
                {
                    //a.InnerHtml = "&laquo;";
                    a.InnerHtml.AppendHtml("&laquo;");
                }
                else if (link.Text == "»")
                {
                    //a.InnerHtml = "&raquo;";
                    a.InnerHtml.AppendHtml("&raquo;");
                }
                else
                {
                    a.InnerHtml.Append(link.Text);
                }

                if (link.Title.Length > 0)
                {
                    a.MergeAttribute("title", link.Title);
                }

                if (AjaxTarget.Length > 0)
                {
                    a.MergeAttribute("data-ajax", "true");
                    a.MergeAttribute("data-ajax-mode", AjaxMode);
                    a.MergeAttribute("data-ajax-update", AjaxTarget);
                }

                li.InnerHtml.Append(a);

                output.Content.Append(li);
            }

            output.Attributes.Clear();
            output.Attributes.Add("class", UlCssClass);
        }
示例#2
0
        /// <summary>
        /// the problem with paging is when you have too many pages
        /// to fit in the pager based on PaginationSettings.MaxPagerItems
        /// you need a strategy to leave out links for some pages
        /// while still being possible to navigate to any page
        /// this class implements one such strategy (implemented by  Martijn Boland)
        /// if you want to implement a different strategy you can plugin your own
        /// IBuildPaginationLinks implementation
        /// </summary>
        /// <param name="paginationSettings"></param>
        /// <param name="generateUrl"></param>
        /// <param name="firstPageText"></param>
        /// <param name="firstPageTitle"></param>
        /// <param name="previousPageText"></param>
        /// <param name="previousPageTitle"></param>
        /// <param name="nextPageText"></param>
        /// <param name="nextPageTitle"></param>
        /// <param name="lastPageText"></param>
        /// <param name="lastPageTitle"></param>
        /// <param name="spacerText"></param>
        /// <returns></returns>
        public List <PaginationLink> BuildPaginationLinks(
            PaginationSettings paginationSettings,
            Func <long, string> generateUrl,
            string firstPageText,
            string firstPageTitle,
            string previousPageText,
            string previousPageTitle,
            string nextPageText,
            string nextPageTitle,
            string lastPageText,
            string lastPageTitle,
            string spacerText = "...")
        {
            List <PaginationLink> paginationLinks = new List <PaginationLink>();

            long totalPages = (long)Math.Ceiling(paginationSettings.TotalItems / (double)paginationSettings.ItemsPerPage);

            // First page
            if (paginationSettings.ShowFirstLast)
            {
                var isActive = paginationSettings.CurrentPage > 1;
                if (isActive || !paginationSettings.SuppressInActiveFirstLast)
                {
                    paginationLinks.Add(
                        new PaginationLink
                    {
                        Active     = isActive,
                        Text       = firstPageText,
                        Title      = firstPageTitle,
                        PageNumber = 1,
                        Url        = generateUrl(1)
                    });
                }
            }

            // Previous page
            if (paginationSettings.UseReverseIncrement)
            {
                var isActive = paginationSettings.CurrentPage < totalPages;

                //SuppressEmptyNextPrev
                if (isActive)
                {
                    paginationLinks.Add(
                        new PaginationLink
                    {
                        Active     = true,
                        PageNumber = paginationSettings.CurrentPage + 1,
                        Text       = previousPageText,
                        Title      = previousPageTitle,
                        Url        = generateUrl(paginationSettings.CurrentPage + 1)
                    });
                }
                else
                {
                    if (!paginationSettings.SuppressEmptyNextPrev)
                    {
                        paginationLinks.Add(
                            new PaginationLink
                        {
                            Active     = false,
                            Text       = previousPageText,
                            PageNumber = totalPages
                        });
                    }
                }
            }
            else
            {
                var isActive = paginationSettings.CurrentPage > 1;
                if (isActive)
                {
                    paginationLinks.Add(new PaginationLink
                    {
                        Active     = true,
                        Text       = previousPageText,
                        Title      = previousPageTitle,
                        PageNumber = paginationSettings.CurrentPage - 1,
                        Url        = generateUrl(paginationSettings.CurrentPage - 1)
                    });
                }
                else
                {
                    if (!paginationSettings.SuppressEmptyNextPrev)
                    {
                        paginationLinks.Add(new PaginationLink
                        {
                            Active     = false,
                            Text       = previousPageText,
                            PageNumber = 1,
                            Url        = generateUrl(1)
                        });
                    }
                }
            }



            if (paginationSettings.ShowNumbered)
            {
                var start = 1L;
                var end   = totalPages;
                var nrOfPagesToDisplay = paginationSettings.MaxPagerItems;

                if (totalPages > nrOfPagesToDisplay)
                {
                    var middle = (long)Math.Ceiling(nrOfPagesToDisplay / 2d) - 1;
                    var below  = (paginationSettings.CurrentPage - middle);
                    var above  = (paginationSettings.CurrentPage + middle);

                    if (below < 2)
                    {
                        above = nrOfPagesToDisplay;
                        below = 1;
                    }
                    else if (above > (totalPages - 2))
                    {
                        above = totalPages;
                        below = (totalPages - nrOfPagesToDisplay + 1);
                    }

                    start = below;
                    end   = above;
                }

                if (start > 1)
                {
                    paginationLinks.Add(new PaginationLink
                    {
                        Active     = true,
                        PageNumber = 1,
                        IsCurrent  = (paginationSettings.CurrentPage == 1 ? true : false),
                        Text       = "1",
                        Url        = generateUrl(1)
                    });

                    if (start > 3)
                    {
                        paginationLinks.Add(new PaginationLink
                        {
                            Active     = true,
                            PageNumber = 2,
                            IsCurrent  = (paginationSettings.CurrentPage == 2 ? true : false),
                            Text       = "2",
                            Url        = generateUrl(2)
                        });
                    }

                    if (start > 2)
                    {
                        paginationLinks.Add(new PaginationLink
                        {
                            Active   = false,
                            Text     = spacerText,
                            IsSpacer = true
                        });
                    }
                }

                for (var i = start; i <= end; i++)
                {
                    if (i == paginationSettings.CurrentPage || (paginationSettings.CurrentPage <= 0 && i == 1))
                    {
                        paginationLinks.Add(new PaginationLink
                        {
                            Active     = true,
                            PageNumber = i,
                            IsCurrent  = (paginationSettings.CurrentPage == i ? true : false),
                            Text       = i.ToString(),
                            Url        = generateUrl(i)
                        });
                    }
                    else
                    {
                        paginationLinks.Add(new PaginationLink
                        {
                            Active     = true,
                            PageNumber = i,
                            Text       = i.ToString(),
                            IsCurrent  = (paginationSettings.CurrentPage == i ? true : false),
                            Url        = generateUrl(i)
                        });
                    }
                }

                if (end < totalPages)
                {
                    if (end < totalPages - 1)
                    {
                        paginationLinks.Add(new PaginationLink
                        {
                            Active   = false,
                            Text     = spacerText,
                            IsSpacer = true
                        });
                    }
                    if (totalPages - 2 > end)
                    {
                        paginationLinks.Add(new PaginationLink
                        {
                            Active     = true,
                            PageNumber = totalPages - 1,
                            Text       = (totalPages - 1).ToString(),
                            IsCurrent  = (paginationSettings.CurrentPage == (totalPages - 1) ? true : false),
                            Url        = generateUrl(totalPages - 1)
                        });
                    }

                    paginationLinks.Add(new PaginationLink
                    {
                        Active     = true,
                        PageNumber = totalPages,
                        Text       = totalPages.ToString(),
                        IsCurrent  = (paginationSettings.CurrentPage == totalPages ? true : false),
                        Url        = generateUrl(totalPages)
                    });
                }
            }



            // Next page
            if (paginationSettings.UseReverseIncrement)
            {
                //SuppressEmptyNextPrev
                var isActive = paginationSettings.CurrentPage > 1;

                if (isActive)
                {
                    paginationLinks.Add(
                        new PaginationLink
                    {
                        Active     = true,
                        Text       = nextPageText,
                        Title      = nextPageTitle,
                        PageNumber = paginationSettings.CurrentPage - 1,
                        Url        = generateUrl(paginationSettings.CurrentPage - 1)
                    });
                }
                else
                {
                    if (!paginationSettings.SuppressEmptyNextPrev)
                    {
                        paginationLinks.Add(
                            new PaginationLink
                        {
                            Active     = false,
                            Text       = nextPageText,
                            PageNumber = 1,
                            Url        = generateUrl(1)
                        });
                    }
                }

                //paginationLinks.Add(
                // paginationSettings.CurrentPage > 1 ? new PaginationLink
                // {
                //     Active = true,
                //     Text = nextPageText,
                //     Title = nextPageTitle,
                //     PageNumber = paginationSettings.CurrentPage + 1,
                //     Url = generateUrl(paginationSettings.CurrentPage + 1)
                // } : new PaginationLink
                // {
                //     Active = false,
                //     Text = nextPageText,
                //     PageNumber = 1,
                //     Url = generateUrl(1)
                // });
            }
            else
            {
                var isActive = paginationSettings.CurrentPage < totalPages;
                if (isActive)
                {
                    paginationLinks.Add(new PaginationLink
                    {
                        Active     = true,
                        PageNumber = paginationSettings.CurrentPage + 1,
                        Text       = nextPageText,
                        Title      = nextPageTitle,
                        Url        = generateUrl(paginationSettings.CurrentPage + 1)
                    });
                }
                else
                {
                    if (!paginationSettings.SuppressEmptyNextPrev)
                    {
                        paginationLinks.Add(new PaginationLink
                        {
                            Active     = false,
                            Text       = nextPageText,
                            PageNumber = totalPages
                        });
                    }
                }
            }


            // Last page
            if (paginationSettings.ShowFirstLast)
            {
                var isActive = paginationSettings.CurrentPage < totalPages;
                if (isActive || !paginationSettings.SuppressInActiveFirstLast)
                {
                    paginationLinks.Add(new PaginationLink
                    {
                        Active     = isActive,
                        Text       = lastPageText,
                        Title      = lastPageTitle,
                        PageNumber = totalPages,
                        Url        = generateUrl(totalPages)
                    });
                }
            }


            return(paginationLinks);
        }