Created
June 7, 2016 11:28
-
-
Save Pzixel/e6fa9fccc020906ac8b2eb0c60e9b9bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
using Microsoft.AspNet.Mvc.Rendering; | |
using Microsoft.AspNet.Razor.TagHelpers; | |
using Microsoft.Extensions.WebEncoders; | |
namespace WebInterface.TagHelpers | |
{ | |
[HtmlTargetElement("pager", Attributes = "total-items, items-per-page")] | |
public class PagerTagHelper : TagHelper | |
{ | |
public int TotalItems { get; set; } | |
public int ItemsPerPage { get; set; } | |
public override void Process(TagHelperContext context, TagHelperOutput output) | |
{ | |
output.TagName = "div"; | |
output.PreContent.SetHtmlContent(""); | |
var items = new StringBuilder(); | |
int totalPages = (int) Math.Ceiling((double) TotalItems/ItemsPerPage); | |
for (var i = 1; i <= totalPages; i++) | |
{ | |
var li = new TagBuilder("li"); | |
var a = new TagBuilder("a"); | |
a.MergeAttribute("href", "javascript:void(0)"); | |
a.MergeAttribute("title", string.Format("Click to go to page {0}", i)); | |
a.InnerHtml.AppendHtml(i.ToString()); | |
if (i == 1) | |
{ | |
a.AddCssClass("active"); | |
} | |
li.InnerHtml.Append(a); | |
var writer = new System.IO.StringWriter(); | |
li.WriteTo(writer, new HtmlEncoder()); | |
var s = writer.ToString(); | |
items.AppendLine(s); | |
} | |
output.Content.SetHtmlContent(items.ToString()); | |
output.PostContent.SetHtmlContent(""); | |
output.Attributes.Clear(); | |
output.Attributes.Add("class", "pager"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment