PagedList.Mvc省略号按钮不起作用

时间:2014-09-06 05:25:10

标签: asp.net-mvc-5 pagedlist

我在我的MVC 5应用程序中使用PagedList.Mvc进行分页。

问题:省略号按钮,在下面的屏幕截图中的第10页之后,在点击时不会执行任何操作。它是应该如何,或者我可以使省略号按钮工作,所以点击它将显示下一组页面?

Ellipsis button does nothing

在View中用于显示此寻呼机的html助手如下所示。

@Html.PagedListPager(Model, page => Url.Action("Index", 
 new { page, sortOrder = ViewBag.CurrentSort, SearchText = ViewBag.SearchText }))

1 个答案:

答案 0 :(得分:2)

有效的解决方案是隐藏省略号按钮。

<强>解

此解决方案涉及隐藏省略号按钮。为此,您需要确保DisplayEllipsesWhenNotShowingAllPageNumbers类下PagedListRenderOptions的属性设置为false,因为默认情况下为true。它的这个设置会导致寻呼机显示省略号按钮。

下面给出的代码片段将进入您的View或PartialView html,您需要更改一些自定义参数,例如sortOrder和action name等。

在寻呼机被激活时隐藏省略号按钮

@Html.PagedListPager(Model,
 page => Url.Action("GetOrderDetails", new { page, sortOrder = ViewBag.CurrentSort,
 , command = "paging", PageSize = ViewBag.PageSize }), 
 PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing( 
 new PagedListRenderOptions { DisplayEllipsesWhenNotShowingAllPageNumbers = false},
 new AjaxOptions() { HttpMethod = "POST", UpdateTargetId = "gridTable",
 OnBegin = "OnBegin", OnSuccess = "OnSuccess" }))

当寻呼机未被禁止时隐藏省略号按钮

@Html.PagedListPager(Model, page => Url.Action("Index", new { page, 
sortOrder = ViewBag.CurrentSort, command = "paging", PageSize = ViewBag.PageSize}),
new PagedListRenderOptions { DisplayEllipsesWhenNotShowingAllPageNumbers = false })

另一种解决方案

https://github.com/troygoode/PagedList的GitHub下载源代码,然后在Visual Studio中打开解决方案。

HtmlHelper.cs 类中添加以下两种方法。

private static TagBuilder PreviousEllipsis(IPagedList list, Func<int, string> generatePageUrl, PagedListRenderOptions options, int firstPageToDisplay)
    {
        var targetPageNumber = firstPageToDisplay - 1;//list.PageNumber - 1;
        var previous = new TagBuilder("a")
        {
            InnerHtml = string.Format(options.EllipsesFormat, targetPageNumber)
        };
        previous.Attributes["rel"] = "prev";

        if (!list.HasPreviousPage)
            return WrapInListItem(previous, options, "PagedList-skipToPrevious","disabled");

        previous.Attributes["href"] = generatePageUrl(targetPageNumber);
        return WrapInListItem(previous, options, "PagedList-skipToPrevious");
    }
    private static TagBuilder NextEllipsis(IPagedList list, Func<int, string> generatePageUrl, PagedListRenderOptions options, int lastPageToDisplay)
    {
        var targetPageNumber = lastPageToDisplay + 1;// list.PageNumber  +1;
        var next = new TagBuilder("a")
        {
            InnerHtml = string.Format(options.EllipsesFormat, targetPageNumber)
        };
        next.Attributes["rel"] = "next";

        if (!list.HasNextPage)
            return   WrapInListItem(next, options, "PagedList-skipToNext", "disabled");

        next.Attributes["href"] = generatePageUrl(targetPageNumber);
        return WrapInListItem(next, options, "PagedList-skipToNext");
    }

在相同的 HtmlHelper.cs 中,使用以下代码替换PagedListPager的现有方法。此代码中只有2个更改,这些更改是在//listItemLinks.Add(Ellipses(options));的注释行之前插入的2行。该方法中有2个位置出现在原始代码中,并且这些位置已经过注释,并被对上面代码片段中定义的新方法的调用所取代。

    ///<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 PagedListPager(this System.Web.Mvc.HtmlHelper html,
                                               IPagedList list,
                                               Func<int, string> generatePageUrl,
                                               PagedListRenderOptions options)
    {
        if (options.Display == PagedListDisplayMode.Never || (options.Display == PagedListDisplayMode.IfNeeded && list.PageCount <= 1))
            return null;

        var listItemLinks = new List<TagBuilder>();

        //calculate start and end of range of page numbers
        var firstPageToDisplay = 1;
        var lastPageToDisplay = list.PageCount;
        var pageNumbersToDisplay = lastPageToDisplay;
        if (options.MaximumPageNumbersToDisplay.HasValue && list.PageCount > options.MaximumPageNumbersToDisplay)
        {
            // cannot fit all pages into pager
            var maxPageNumbersToDisplay = options.MaximumPageNumbersToDisplay.Value;
            firstPageToDisplay = list.PageNumber - maxPageNumbersToDisplay / 2;
            if (firstPageToDisplay < 1)
                firstPageToDisplay = 1;
            pageNumbersToDisplay = maxPageNumbersToDisplay;
            lastPageToDisplay = firstPageToDisplay + pageNumbersToDisplay - 1;
            if (lastPageToDisplay > list.PageCount)
                firstPageToDisplay = list.PageCount - maxPageNumbersToDisplay + 1;
        }


        //first
        if (options.DisplayLinkToFirstPage == PagedListDisplayMode.Always || (options.DisplayLinkToFirstPage == PagedListDisplayMode.IfNeeded && firstPageToDisplay > 1))
            listItemLinks.Add(First(list, generatePageUrl, options));

        //previous
        if (options.DisplayLinkToPreviousPage == PagedListDisplayMode.Always || (options.DisplayLinkToPreviousPage == PagedListDisplayMode.IfNeeded && !list.IsFirstPage))
            listItemLinks.Add(Previous(list, generatePageUrl, options));

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

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

        //page
        if (options.DisplayLinkToIndividualPages)
        {
            //if there are previous page numbers not displayed, show an ellipsis
            if (options.DisplayEllipsesWhenNotShowingAllPageNumbers && firstPageToDisplay > 1)
                listItemLinks.Add(PreviousEllipsis(list, generatePageUrl, options, firstPageToDisplay));
                //listItemLinks.Add(Ellipses(options));

            foreach (var i in Enumerable.Range(firstPageToDisplay, pageNumbersToDisplay))
            {
                //show delimiter between page numbers
                if (i > firstPageToDisplay && !string.IsNullOrWhiteSpace(options.DelimiterBetweenPageNumbers))
                    listItemLinks.Add(WrapInListItem(options.DelimiterBetweenPageNumbers));

                //show page number link
                listItemLinks.Add(Page(i, list, generatePageUrl, options));
            }

            //if there are subsequent page numbers not displayed, show an ellipsis
            if (options.DisplayEllipsesWhenNotShowingAllPageNumbers && (firstPageToDisplay + pageNumbersToDisplay - 1) < list.PageCount)
                listItemLinks.Add(NextEllipsis(list, generatePageUrl, options, lastPageToDisplay));
                //listItemLinks.Add(Ellipses(options));
        }

        //next
        if (options.DisplayLinkToNextPage == PagedListDisplayMode.Always || (options.DisplayLinkToNextPage == PagedListDisplayMode.IfNeeded && !list.IsLastPage))
            listItemLinks.Add(Next(list, generatePageUrl, options));

        //last
        if (options.DisplayLinkToLastPage == PagedListDisplayMode.Always || (options.DisplayLinkToLastPage == PagedListDisplayMode.IfNeeded && lastPageToDisplay < list.PageCount))
            listItemLinks.Add(Last(list, generatePageUrl, options));

        if(listItemLinks.Any())
        {
            //append class to first item in list?
            if (!string.IsNullOrWhiteSpace(options.ClassToApplyToFirstListItemInPager))
                listItemLinks.First().AddCssClass(options.ClassToApplyToFirstListItemInPager);

            //append class to last item in list?
            if (!string.IsNullOrWhiteSpace(options.ClassToApplyToLastListItemInPager))
                listItemLinks.Last().AddCssClass(options.ClassToApplyToLastListItemInPager);

            //append classes to all list item links
            foreach (var li in listItemLinks)
                foreach (var c in options.LiElementClasses ?? Enumerable.Empty<string>())
                    li.AddCssClass(c);
        }

        //collapse all of the list items into one big string
        var listItemLinksString = listItemLinks.Aggregate(
            new StringBuilder(),
            (sb, listItem) => sb.Append(listItem.ToString()),
            sb=> sb.ToString()
            );

        var ul = new TagBuilder("ul")
                    {
                        InnerHtml = listItemLinksString
                    };
        foreach (var c in options.UlElementClasses ?? Enumerable.Empty<string>())
            ul.AddCssClass(c);

        var outerDiv = new TagBuilder("div");
        foreach(var c in options.ContainerDivClasses ?? Enumerable.Empty<string>())
            outerDiv.AddCssClass(c);
        outerDiv.InnerHtml = ul.ToString();

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

然后,重建代码并将dll复制到bin文件夹。在此之后,您将发现省略号按钮已启用,并将带您到当前页码集的第一页之前的页面,或刚刚此当前集之后的页面。我对此进行了测试并且有效。