gpt4 book ai didi

asp.net-mvc-5 - PagedList.Mvc 省略号按钮不起作用

转载 作者:行者123 更新时间:2023-12-01 03:47:22 24 4
gpt4 key购买 nike

我在我的 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 }))

最佳答案

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

解决方案

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

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

当 Pager ajaxified 时隐藏省略号按钮

@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" }))

当 Pager 非ajaxified 时隐藏省略号按钮
@Html.PagedListPager(Model, page => Url.Action("Index", new { page, 
sortOrder = ViewBag.CurrentSort, command = "paging", PageSize = ViewBag.PageSize}),
new PagedListRenderOptions { DisplayEllipsesWhenNotShowingAllPageNumbers = false })

另一个解决方案

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

在 HtmlHelper.cs 类中添加以下 2 个方法。
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 文件夹中。之后,您会发现省略号按钮已启用,它将带您进入当前页码集第一页之前的页面,或当前页码之后的页面。我对此进行了测试,并且奏效了。

关于asp.net-mvc-5 - PagedList.Mvc 省略号按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25697118/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com