Skip to content

Commit

Permalink
Pager class implements IHtmlString.
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnboland committed Apr 19, 2012
1 parent 928bee5 commit 1571658
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 46 deletions.
56 changes: 27 additions & 29 deletions src/MvcPaging/Pager.cs
Expand Up @@ -9,7 +9,7 @@

namespace MvcPaging
{
public class Pager
public class Pager : IHtmlString
{
private ViewContext viewContext;
private readonly int pageSize;
Expand Down Expand Up @@ -106,37 +106,35 @@ public IList<PaginationModel> BuildPaginationModel()
return pages;
}

public HtmlString RenderHtml()
{
public string ToHtmlString()
{
var sb = new StringBuilder();

var pages = BuildPaginationModel();
var pages = BuildPaginationModel();

foreach (var page in pages)
{
if (page.Active)
{
if (page.IsCurrent)
{
sb.AppendFormat("<span class=\"current\">{0}</span>", page.DisplayText);
}
else if (!page.PageIndex.HasValue)
{
sb.AppendFormat(page.DisplayText);
}
else
{
sb.Append(GeneratePageLink(page.DisplayText, page.PageIndex.GetValueOrDefault()));
}
}
else
{
sb.AppendFormat("<span class=\"disabled\">{0}</span>", page.DisplayText);
}
}


return new HtmlString(sb.ToString());
foreach (var page in pages)
{
if (page.Active)
{
if (page.IsCurrent)
{
sb.AppendFormat("<span class=\"current\">{0}</span>", page.DisplayText);
}
else if (!page.PageIndex.HasValue)
{
sb.AppendFormat(page.DisplayText);
}
else
{
sb.Append(GeneratePageLink(page.DisplayText, page.PageIndex.GetValueOrDefault()));
}
}
else
{
sb.AppendFormat("<span class=\"disabled\">{0}</span>", page.DisplayText);
}
}
return sb.ToString();
}

private string GeneratePageLink(string linkText, int pageNumber)
Expand Down
31 changes: 14 additions & 17 deletions src/MvcPaging/PagingExtensions.cs
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Web.Routing;
Expand All @@ -12,32 +11,32 @@ public static class PagingExtensions
{
#region AjaxHelper extensions

public static HtmlString Pager(this AjaxHelper ajaxHelper, int pageSize, int currentPage, int totalItemCount, AjaxOptions ajaxOptions)
public static Pager Pager(this AjaxHelper ajaxHelper, int pageSize, int currentPage, int totalItemCount, AjaxOptions ajaxOptions)
{
return Pager(ajaxHelper, pageSize, currentPage, totalItemCount, null, null, ajaxOptions);
}

public static HtmlString Pager(this AjaxHelper ajaxHelper, int pageSize, int currentPage, int totalItemCount, string actionName, AjaxOptions ajaxOptions)
public static Pager Pager(this AjaxHelper ajaxHelper, int pageSize, int currentPage, int totalItemCount, string actionName, AjaxOptions ajaxOptions)
{
return Pager(ajaxHelper, pageSize, currentPage, totalItemCount, actionName, null, ajaxOptions);
}

public static HtmlString Pager(this AjaxHelper ajaxHelper, int pageSize, int currentPage, int totalItemCount, object values, AjaxOptions ajaxOptions)
public static Pager Pager(this AjaxHelper ajaxHelper, int pageSize, int currentPage, int totalItemCount, object values, AjaxOptions ajaxOptions)
{
return Pager(ajaxHelper, pageSize, currentPage, totalItemCount, null, new RouteValueDictionary(values), ajaxOptions);
}

public static HtmlString Pager(this AjaxHelper ajaxHelper, int pageSize, int currentPage, int totalItemCount, string actionName, object values, AjaxOptions ajaxOptions)
public static Pager Pager(this AjaxHelper ajaxHelper, int pageSize, int currentPage, int totalItemCount, string actionName, object values, AjaxOptions ajaxOptions)
{
return Pager(ajaxHelper, pageSize, currentPage, totalItemCount, actionName, new RouteValueDictionary(values), ajaxOptions);
}

public static HtmlString Pager(this AjaxHelper ajaxHelper, int pageSize, int currentPage, int totalItemCount, RouteValueDictionary valuesDictionary, AjaxOptions ajaxOptions)
public static Pager Pager(this AjaxHelper ajaxHelper, int pageSize, int currentPage, int totalItemCount, RouteValueDictionary valuesDictionary, AjaxOptions ajaxOptions)
{
return Pager(ajaxHelper, pageSize, currentPage, totalItemCount, null, valuesDictionary, ajaxOptions);
}

public static HtmlString Pager(this AjaxHelper ajaxHelper, int pageSize, int currentPage, int totalItemCount, string actionName, RouteValueDictionary valuesDictionary, AjaxOptions ajaxOptions)
public static Pager Pager(this AjaxHelper ajaxHelper, int pageSize, int currentPage, int totalItemCount, string actionName, RouteValueDictionary valuesDictionary, AjaxOptions ajaxOptions)
{
if (valuesDictionary == null)
{
Expand All @@ -51,40 +50,39 @@ public static HtmlString Pager(this AjaxHelper ajaxHelper, int pageSize, int cur
}
valuesDictionary.Add("action", actionName);
}
var pager = new Pager(ajaxHelper.ViewContext, pageSize, currentPage, totalItemCount, valuesDictionary, ajaxOptions);
return pager.RenderHtml();
return new Pager(ajaxHelper.ViewContext, pageSize, currentPage, totalItemCount, valuesDictionary, ajaxOptions);
}

#endregion

#region HtmlHelper extensions

public static HtmlString Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount)
public static Pager Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount)
{
return Pager(htmlHelper, pageSize, currentPage, totalItemCount, null, null);
}

public static HtmlString Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, string actionName)
public static Pager Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, string actionName)
{
return Pager(htmlHelper, pageSize, currentPage, totalItemCount, actionName, null);
}

public static HtmlString Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, object values)
public static Pager Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, object values)
{
return Pager(htmlHelper, pageSize, currentPage, totalItemCount, null, new RouteValueDictionary(values));
}

public static HtmlString Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, string actionName, object values)
public static Pager Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, string actionName, object values)
{
return Pager(htmlHelper, pageSize, currentPage, totalItemCount, actionName, new RouteValueDictionary(values));
}

public static HtmlString Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, RouteValueDictionary valuesDictionary)
public static Pager Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, RouteValueDictionary valuesDictionary)
{
return Pager(htmlHelper, pageSize, currentPage, totalItemCount, null, valuesDictionary);
}

public static HtmlString Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, string actionName, RouteValueDictionary valuesDictionary)
public static Pager Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, string actionName, RouteValueDictionary valuesDictionary)
{
if (valuesDictionary == null)
{
Expand All @@ -98,8 +96,7 @@ public static HtmlString Pager(this HtmlHelper htmlHelper, int pageSize, int cur
}
valuesDictionary.Add("action", actionName);
}
var pager = new Pager(htmlHelper.ViewContext, pageSize, currentPage, totalItemCount, valuesDictionary, null);
return pager.RenderHtml();
return new Pager(htmlHelper.ViewContext, pageSize, currentPage, totalItemCount, valuesDictionary, null);
}

public static IEnumerable<PaginationModel> PagerModel(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, string actionName, RouteValueDictionary valuesDictionary)
Expand Down

0 comments on commit 1571658

Please sign in to comment.