Esempio n. 1
0
        ///<summary>
        ///	Displays a configurable paging control for instances of PagedList.
        ///</summary>
        ///<param name = "html">This method is meant to hook off HtmlHelper as an extension method.</param>
        ///<param name = "list">The PagedList to use as the data source.</param>
        ///<param name = "generatePageUrl">A function that takes the page number  of the desired page and returns a URL-string that will load that page.</param>
        ///<param name = "options">Formatting options.</param>
        ///<returns>Outputs the paging control HTML.</returns>
        public static MvcHtmlString PageCurrentPage(this HtmlHelper html,
                                                    IPagedList list,
                                                    Func <int, object> getRouteValues,
                                                    PagedListRenderOptions options)
        {
            if (list.PageCount <= 1 && options.HideIf1Page)
            {
                return(MvcHtmlString.Create("&nbsp;"));
            }

            var listItemLinks = new StringBuilder();

            //first
            if (options.DisplayLinkToFirstPage)
            {
                listItemLinks.Append(First(html, list, getRouteValues, options.LinkToFirstPageFormat));
            }

            //previous
            if (options.DisplayLinkToPreviousPage)
            {
                listItemLinks.Append(Previous(html, list, getRouteValues, options.LinkToPreviousPageFormat));
            }

            //text
            if (options.DisplayPageCountAndCurrentLocation)
            {
                listItemLinks.Append(PageCountAndLocationText(list, options.PageCountAndCurrentLocationFormat));
            }

            //text
            if (options.DisplayItemSliceAndTotal)
            {
                listItemLinks.Append(ItemSliceAndTotalText(list, options.ItemSliceAndTotalFormat));
            }

            //page
            if (options.DisplayLinkToIndividualPages)
            {
                //calculate start and end of range of page numbers
                int start = 1;
                int end   = list.PageCount;
                if (options.MaximumPageNumbersToDisplay.HasValue && list.PageCount > options.MaximumPageNumbersToDisplay)
                {
                    int maxPageNumbersToDisplay = options.MaximumPageNumbersToDisplay.Value;
                    start = list.PageNumber - maxPageNumbersToDisplay / 2;
                    if (start < 1)
                    {
                        start = 1;
                    }
                    end = maxPageNumbersToDisplay;
                    if ((start + end - 1) > list.PageCount)
                    {
                        start = list.PageCount - maxPageNumbersToDisplay + 1;
                    }
                }

                //if there are previous page numbers not displayed, show an ellipsis
                if (options.DisplayEllipsesWhenNotShowingAllPageNumbers && start > 1)
                {
                    listItemLinks.Append(Ellipses(options.EllipsesFormat));
                }

                foreach (int i in Enumerable.Range(start, end))
                {
                    //show delimiter between page numbers
                    if (i > start && !string.IsNullOrWhiteSpace(options.DelimiterBetweenPageNumbers))
                    {
                        listItemLinks.Append(options.DelimiterBetweenPageNumbers);
                    }

                    //show page number link
                    listItemLinks.Append(options.FunctionToDisplayEachPageNumber == null
                                             ? Page(html, i, list, getRouteValues,
                                                    options.LinkToIndividualPageFormat)
                                             : Page(html, i, list, getRouteValues,
                                                    options.FunctionToDisplayEachPageNumber));
                }

                //if there are subsequent page numbers not displayed, show an ellipsis
                if (options.DisplayEllipsesWhenNotShowingAllPageNumbers && (start + end - 1) < list.PageCount)
                {
                    listItemLinks.Append(Ellipses(options.EllipsesFormat));
                }
            }

            //next
            if (options.DisplayLinkToNextPage)
            {
                listItemLinks.Append(Next(html, list, getRouteValues, options.LinkToNextPageFormat));
            }

            //last
            if (options.DisplayLinkToLastPage)
            {
                listItemLinks.Append(Last(html, list, getRouteValues, options.LinkToLastPageFormat));
            }
            var ul = new TagBuilder("ul")
            {
                InnerHtml = listItemLinks.ToString()
            };

            ul.AddCssClass("pagination"); //bootstrap3

            var div = new TagBuilder("div")
            {
                InnerHtml = ul.ToString()
            };

            div.AddCssClass("pagination"); //bootstrap2

            return(new MvcHtmlString(div.ToString()));
        }
Esempio n. 2
0
        ///<summary>
        ///	Displays a configurable paging control for instances of PagedList.
        ///</summary>
        ///<param name = "html">This method is meant to hook off HtmlHelper as an extension method.</param>
        ///<param name = "list">The PagedList to use as the data source.</param>
        ///<param name = "generatePageUrl">A function that takes the page number  of the desired page and returns a URL-string that will load that page.</param>
        ///<param name = "options">Formatting options.</param>
        ///<returns>Outputs the paging control HTML.</returns>
        public static MvcHtmlString PageCurrentPage(this HtmlHelper html,
                                          IPagedList list,
                                          Func<int, object> getRouteValues,
                                          PagedListRenderOptions options)
        {
            if (list.PageCount <= 1 && options.HideIf1Page)
                return MvcHtmlString.Create("&nbsp;");

            var listItemLinks = new StringBuilder();

            //first
            if (options.DisplayLinkToFirstPage)
                listItemLinks.Append(First(html, list, getRouteValues, options.LinkToFirstPageFormat));

            //previous
            if (options.DisplayLinkToPreviousPage)
                listItemLinks.Append(Previous(html, list, getRouteValues, options.LinkToPreviousPageFormat));

            //text
            if (options.DisplayPageCountAndCurrentLocation)
                listItemLinks.Append(PageCountAndLocationText(list, options.PageCountAndCurrentLocationFormat));

            //text
            if (options.DisplayItemSliceAndTotal)
                listItemLinks.Append(ItemSliceAndTotalText(list, options.ItemSliceAndTotalFormat));

            //page
            if (options.DisplayLinkToIndividualPages)
            {
                //calculate start and end of range of page numbers
                int start = 1;
                int end = list.PageCount;
                if (options.MaximumPageNumbersToDisplay.HasValue && list.PageCount > options.MaximumPageNumbersToDisplay)
                {
                    int maxPageNumbersToDisplay = options.MaximumPageNumbersToDisplay.Value;
                    start = list.PageNumber - maxPageNumbersToDisplay / 2;
                    if (start < 1)
                        start = 1;
                    end = maxPageNumbersToDisplay;
                    if ((start + end - 1) > list.PageCount)
                        start = list.PageCount - maxPageNumbersToDisplay + 1;
                }

                //if there are previous page numbers not displayed, show an ellipsis
                if (options.DisplayEllipsesWhenNotShowingAllPageNumbers && start > 1)
                    listItemLinks.Append(Ellipses(options.EllipsesFormat));

                foreach (int i in Enumerable.Range(start, end))
                {
                    //show delimiter between page numbers
                    if (i > start && !string.IsNullOrWhiteSpace(options.DelimiterBetweenPageNumbers))
                        listItemLinks.Append(options.DelimiterBetweenPageNumbers);

                    //show page number link
                    listItemLinks.Append(options.FunctionToDisplayEachPageNumber == null
                                             ? Page(html, i, list, getRouteValues,
                                                    options.LinkToIndividualPageFormat)
                                             : Page(html, i, list, getRouteValues,
                                                    options.FunctionToDisplayEachPageNumber));
                }

                //if there are subsequent page numbers not displayed, show an ellipsis
                if (options.DisplayEllipsesWhenNotShowingAllPageNumbers && (start + end - 1) < list.PageCount)
                    listItemLinks.Append(Ellipses(options.EllipsesFormat));
            }

            //next
            if (options.DisplayLinkToNextPage)
                listItemLinks.Append(Next(html, list, getRouteValues, options.LinkToNextPageFormat));

            //last
            if (options.DisplayLinkToLastPage)
                listItemLinks.Append(Last(html, list, getRouteValues, options.LinkToLastPageFormat));
            var ul = new TagBuilder("ul")
                         {
                             InnerHtml = listItemLinks.ToString()
                         };
            ul.AddCssClass("pagination"); //bootstrap3

            var div = new TagBuilder("div")
                          {
                              InnerHtml = ul.ToString()
                          };
            div.AddCssClass("pagination"); //bootstrap2

            return new MvcHtmlString(div.ToString());
        }