Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 40 additions & 15 deletions src/PagedList.Mvc/HtmlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private static TagBuilder WrapInListItem(TagBuilder inner, PagedListRenderOption
return li;
}

private static TagBuilder First(IPagedList list, Func<int, string> generatePageUrl, PagedListRenderOptions options)
private static TagBuilder First(IPagedList list, Func<int, string> generatePageUrl, Func<int, string> generateAjaxUrl, PagedListRenderOptions options)
{
const int targetPageNumber = 1;
var first = new TagBuilder("a")
Expand All @@ -41,10 +41,11 @@ private static TagBuilder First(IPagedList list, Func<int, string> generatePageU
return WrapInListItem(first, options, "PagedList-skipToFirst", "disabled");

first.Attributes["href"] = generatePageUrl(targetPageNumber);
if (generateAjaxUrl != null) { first.Attributes[options.UnobtrusiveAjaxAttrName] = generateAjaxUrl(targetPageNumber); }
return WrapInListItem(first, options, "PagedList-skipToFirst");
}

private static TagBuilder Previous(IPagedList list, Func<int, string> generatePageUrl, PagedListRenderOptions options)
private static TagBuilder Previous(IPagedList list, Func<int, string> generatePageUrl, Func<int, string> generateAjaxUrl, PagedListRenderOptions options)
{
var targetPageNumber = list.PageNumber - 1;
var previous = new TagBuilder("a")
Expand All @@ -57,10 +58,11 @@ private static TagBuilder Previous(IPagedList list, Func<int, string> generatePa
return WrapInListItem(previous, options, "PagedList-skipToPrevious", "disabled");

previous.Attributes["href"] = generatePageUrl(targetPageNumber);
if (generateAjaxUrl != null) { previous.Attributes[options.UnobtrusiveAjaxAttrName] = generateAjaxUrl(targetPageNumber); }
return WrapInListItem(previous, options, "PagedList-skipToPrevious");
}

private static TagBuilder Page(int i, IPagedList list, Func<int, string> generatePageUrl, PagedListRenderOptions options)
private static TagBuilder Page(int i, IPagedList list, Func<int, string> generatePageUrl, Func<int, string> generateAjaxUrl, PagedListRenderOptions options)
{
var format = options.FunctionToDisplayEachPageNumber
?? (pageNumber => string.Format(options.LinkToIndividualPageFormat, pageNumber));
Expand All @@ -72,10 +74,11 @@ private static TagBuilder Page(int i, IPagedList list, Func<int, string> generat
return WrapInListItem(page, options, "active");

page.Attributes["href"] = generatePageUrl(targetPageNumber);
if (generateAjaxUrl != null) { page.Attributes[options.UnobtrusiveAjaxAttrName] = generateAjaxUrl(targetPageNumber); }
return WrapInListItem(page, options);
}

private static TagBuilder Next(IPagedList list, Func<int, string> generatePageUrl, PagedListRenderOptions options)
private static TagBuilder Next(IPagedList list, Func<int, string> generatePageUrl, Func<int, string> generateAjaxUrl, PagedListRenderOptions options)
{
var targetPageNumber = list.PageNumber + 1;
var next = new TagBuilder("a")
Expand All @@ -88,10 +91,11 @@ private static TagBuilder Next(IPagedList list, Func<int, string> generatePageUr
return WrapInListItem(next, options, "PagedList-skipToNext", "disabled");

next.Attributes["href"] = generatePageUrl(targetPageNumber);
if (generateAjaxUrl != null) { next.Attributes[options.UnobtrusiveAjaxAttrName] = generateAjaxUrl(targetPageNumber); }
return WrapInListItem(next, options, "PagedList-skipToNext");
}

private static TagBuilder Last(IPagedList list, Func<int, string> generatePageUrl, PagedListRenderOptions options)
private static TagBuilder Last(IPagedList list, Func<int, string> generatePageUrl, Func<int, string> generateAjaxUrl, PagedListRenderOptions options)
{
var targetPageNumber = list.PageCount;
var last = new TagBuilder("a")
Expand All @@ -103,6 +107,7 @@ private static TagBuilder Last(IPagedList list, Func<int, string> generatePageUr
return WrapInListItem(last, options, "PagedList-skipToLast", "disabled");

last.Attributes["href"] = generatePageUrl(targetPageNumber);
if (generateAjaxUrl != null) { last.Attributes[options.UnobtrusiveAjaxAttrName] = generateAjaxUrl(targetPageNumber); }
return WrapInListItem(last, options, "PagedList-skipToLast");
}

Expand Down Expand Up @@ -138,25 +143,45 @@ private static TagBuilder Ellipses(PagedListRenderOptions options)
///<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>
///<returns>Outputs the paging control HTML.</returns>
///<param name = "generateAjaxUrl">A function that takes the page number of the desired page and returns a URL-string that using for unobtrusive ajax.</param>
///<returns>Outputs the paging control HTML.</returns>
public static MvcHtmlString PagedListPager(this System.Web.Mvc.HtmlHelper html,
IPagedList list,
Func<int, string> generatePageUrl)
Func<int, string> generatePageUrl,
Func<int, string> generateAjaxUrl = null)
{
return PagedListPager(html, list, generatePageUrl, new PagedListRenderOptions());
return PagedListPager(html, list, generatePageUrl, generateAjaxUrl, new PagedListRenderOptions());
}

///<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)
{
return PagedListPager(html, list, generatePageUrl, null, options);
}

///<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>
///<param name = "generateAjaxUrl">A function that takes the page number of the desired page and returns a URL-string that using for unobtrusive ajax.</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,
Func<int, string> generatePageUrl,
Func<int, string> generateAjaxUrl,
PagedListRenderOptions options)
{
if (options.Display == PagedListDisplayMode.Never || (options.Display == PagedListDisplayMode.IfNeeded && list.PageCount <= 1))
Expand All @@ -183,11 +208,11 @@ public static MvcHtmlString PagedListPager(this System.Web.Mvc.HtmlHelper html,

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

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

//text
if (options.DisplayPageCountAndCurrentLocation)
Expand All @@ -211,7 +236,7 @@ public static MvcHtmlString PagedListPager(this System.Web.Mvc.HtmlHelper html,
listItemLinks.Add(WrapInListItem(options.DelimiterBetweenPageNumbers));

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

//if there are subsequent page numbers not displayed, show an ellipsis
Expand All @@ -221,11 +246,11 @@ public static MvcHtmlString PagedListPager(this System.Web.Mvc.HtmlHelper html,

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

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

if(listItemLinks.Any())
{
Expand Down
6 changes: 6 additions & 0 deletions src/PagedList.Mvc/PagedListRenderOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public PagedListRenderOptions()
ContainerDivClasses = new [] { "pagination-container" };
UlElementClasses = new[] { "pagination" };
LiElementClasses = Enumerable.Empty<string>();
UnobtrusiveAjaxAttrName = "data-ajax-url";
}

///<summary>
Expand Down Expand Up @@ -205,6 +206,11 @@ public PagedListRenderOptions()
/// </summary>
public Func<TagBuilder, TagBuilder, TagBuilder> FunctionToTransformEachPageLink { get; set; }

/// <summary>
/// Specifies an attribute name to append generated unobtrusive ajax url
/// </summary>
public string UnobtrusiveAjaxAttrName { get; set; }

/// <summary>
/// Enables ASP.NET MVC's unobtrusive AJAX feature. An XHR request will retrieve HTML from the clicked page and replace the innerHtml of the provided element ID.
/// </summary>
Expand Down