public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int, string> pageUrl) { var result = new StringBuilder(); for (int i = 1; i <= pagingInfo.TotalPages; i++) { var tag = new TagBuilder("a"); tag.MergeAttribute("href", pageUrl(i)); // Func<int, string> // int here is just the input type, 1 parameter // string is what is returned. // <a href="computedFunc pageUrl"> tag.InnerHtml = i.ToString(); //i if (i == pagingInfo.CurrentPage) { tag.AddCssClass("selected"); tag.AddCssClass("btn-primary"); } tag.AddCssClass("btn btn-default"); result.Append(tag.ToString()); } return MvcHtmlString.Create(result.ToString()); }
public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int, string> pageUrl) { StringBuilder result = new StringBuilder(); for (int i = 1; i <= pagingInfo.TotalPages; i++) { TagBuilder tag = new TagBuilder("a"); // Construct an <a> tag tag.MergeAttribute("href", pageUrl(i)); tag.InnerHtml = i.ToString(); if (i == pagingInfo.CurrentPage) tag.AddCssClass("selected"); result.Append(tag.ToString()); } return MvcHtmlString.Create(result.ToString()); }
public void Can_Generate_Page_Links() { HtmlHelper myHelper = null; PagingInfo pagingInfo = new PagingInfo { CurrentPage = 2, TotalItems = 28, ItemsPerPage = 10 }; Func<int, string> pageUrlDelegate = i => "Page" + i; MvcHtmlString result = myHelper.PageLinks(pagingInfo, pageUrlDelegate); Assert.AreEqual(@"<a class=""btn btn-default"" href=""Page1"">1</a><a class=""btn btn-primary selected"" href=""Page2"">2</a><a class=""btn btn-default"" href=""Page3"">3</a>", result.ToString()); }
public void PagingHelpers_Can_Genrate_PagingLinks() { //Arrange HtmlHelper myhelper = null; // TODO: Initialize to an appropriate value PagingInfo pagingInfo = new PagingInfo { CurrentPage = 2, TotalItems = 30, ItemsPerPage = 10 }; Func<int, string> pageUrlDelegate = (x=>"Page"+x); // TODO: Initialize to an appropriate value //Act MvcHtmlString result = myhelper.PageLinks(pagingInfo, pageUrlDelegate); //Assert string expected = @"<a href=""Page1"">1</a><a class=""selected"" href=""Page2"">2</a><a href=""Page3"">3</a>"; Assert.AreEqual(result.ToString(), expected); }
public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int, string> pageUrl) { var result = new StringBuilder(); for (var index = 1; index <= pagingInfo.TotalPages; index++) { var tag = new TagBuilder("a"); tag.MergeAttribute("href", pageUrl(index)); tag.InnerHtml = index.ToString(); if (index == pagingInfo.CurrentPage) tag.AddCssClass("selected"); result.Append(tag); } return MvcHtmlString.Create(result.ToString()); }
public void Can_Generate_Links_To_Other_Pages() { HtmlHelper html = null; PagingInfo pagingInfo = new PagingInfo { CurrentPage =2, TotalItems=28, ItemPerPage=10 }; Func<int, string> pageUrl = i => "Page" + i.ToString(); MvcHtmlString result = html.PageLinks(pagingInfo, pageUrl); result.ToString().ShouldEqual(@"<a href=""Page1"">1</a> <a class=""selected"" href=""Page2"">2</a> <a href=""Page3"">3</a> "); }
public static MvcHtmlString PageLinks(this HtmlHelper helper, PagingInfo info, Func<int, string> pageUrlFunction ) { var result = new StringBuilder(); for (int i = 1; i <= info.TotalPages; i++) { var tag = new TagBuilder("a"); tag.MergeAttribute("href", pageUrlFunction(i)); tag.InnerHtml = i.ToString(); if(i == info.CurrentPage) { tag.AddCssClass("selected"); } result.Append(tag.ToString()); } return MvcHtmlString.Create(result.ToString()); }
public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int, string> pageUrl) { StringBuilder pageLinkResult = new StringBuilder(); for (int pageNumber = 1; pageNumber <= pagingInfo.TotalPages; pageNumber++) { TagBuilder tagResult = new TagBuilder("a"); tagResult.MergeAttribute("href", pageUrl(pageNumber)); tagResult.InnerHtml = pageNumber.ToString(); if (pageNumber == pagingInfo.CurrentPage) tagResult.AddCssClass("selected"); pageLinkResult.Append(tagResult.ToString()); } return MvcHtmlString.Create(pageLinkResult.ToString()); }
public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int, string> pageUrl) { var stringBuilder = new StringBuilder(); for (int i = 1; i <= pagingInfo.TotalPages; i++) { var tagBuilder = new TagBuilder("a"); tagBuilder.MergeAttribute("href", pageUrl(i)); tagBuilder.InnerHtml = i.ToString(); if (i == pagingInfo.CurrentPage) { tagBuilder.AddCssClass("selected"); } stringBuilder.Append(tagBuilder); } return MvcHtmlString.Create(stringBuilder.ToString()); }
public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int, string> pageUrl) { var result = new StringBuilder(); for (var i = 1; i <= pagingInfo.TotalPages; i++) { var tag = new TagBuilder("a"); tag.MergeAttribute("href", pageUrl(i)); // ReSharper disable SpecifyACultureInStringConversionExplicitly tag.InnerHtml = i.ToString(); // ReSharper restore SpecifyACultureInStringConversionExplicitly if (i == pagingInfo.CurrentPage) { tag.AddCssClass("selected"); } result.Append(tag); } return MvcHtmlString.Create(result.ToString()); }
public void CanGeneratePageLinks() { HtmlHelper helper = null; var pagingInfo = new PagingInfo { CurrentPage = 2, TotalItems = 28, ItemsPerPage = 10 }; Func<int, string> pageUrlDelegate = i => "Page " + i; MvcHtmlString result = helper.PageLinks(pagingInfo, pageUrlDelegate); var expected = @"<a href=""Page 1"">1</a>" + @"<a class=""selected"" href=""Page 2"">2</a>" + @"<a href=""Page 3"">3</a>"; Assert.AreEqual(expected, result.ToString()); }
public void Can_Generate_Page_Links() { // Arrange HtmlHelper myHelper = null; PagingInfo pagingInfo = new PagingInfo { CurrentPage = 2, TotalItems = 28, ItemsPerPage = 10 }; Func<Int32, String> pageUrlDelegate = i => "Page" + i; // Act MvcHtmlString result = myHelper.PageLinks(pagingInfo, pageUrlDelegate); // Assert Assert.AreEqual(@"<a href=""Page1"">1</a><a class=""selected"" href=""Page2"">2</a><a href=""Page3"">3</a>", result.ToString()); }
public void Can_Generate_Page_Links() { // Arrange - define an HTML helper - we need to do this // in order to apply the extension method HtmlHelper myHelper = null; // Arrange - create PagingInfo data PagingInfo pagingInfo = new PagingInfo { CurrentPage = 2, TotalItems = 28, ItemsPerPage = 10 }; // Arrange - set up the delegate using a lambda expression Func<int, string> pageUrlDelegate = i => "Page" + i; // Act MvcHtmlString result = myHelper.PageLinks(pagingInfo, pageUrlDelegate); // Assert Assert.AreEqual(result.ToString(), @"<a href=""Page1"">1</a><a class=""selected"" href=""Page2"">2</a><a href=""Page3"">3</a>"); }
public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int, string> pageUrl) { var result = new StringBuilder(); for (var i = 1; i <= pagingInfo.TotalPage; i++) { var tag = new TagBuilder("a"); tag.MergeAttribute("href", pageUrl(i)); tag.InnerHtml = i.ToString(); if (i == pagingInfo.CurrentPage) { tag.AddCssClass("selected"); tag.AddCssClass("btn-primary"); } tag.AddCssClass("btn btn-default"); result.Append(tag); } return MvcHtmlString.Create(result.ToString()); }
public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int, string> pageUrl) { StringBuilder resultHtml = new StringBuilder(); StringBuilder liHtml = new StringBuilder(); double tempTotalPages = pagingInfo.TotalItems / pagingInfo.ItemsPerPage; double totalPages = Math.Floor(tempTotalPages); //Final HTML markup //<ul class="pagination"> // <li><a href="Page1">1</a> // <li><a href="Page2">2</a> //</ul> //Create <ul></ul> tag TagBuilder ulTag = new TagBuilder("ul"); ulTag.AddCssClass("pagination"); for (int i = 1; i <= totalPages+1; i++) { StringBuilder aHtml = new StringBuilder(); //Create <a></a> tag TagBuilder aTag = new TagBuilder("a"); aTag.MergeAttribute("href",pageUrl(i)); aTag.InnerHtml = i.ToString(); //create li tag and add <a> tag into it TagBuilder liTag = new TagBuilder("li"); liTag.InnerHtml = aTag.ToString(); if (i == pagingInfo.CurrentPage) { liTag.AddCssClass("active"); } liHtml.Append(liTag.ToString()); } ulTag.InnerHtml = liHtml.ToString(); return MvcHtmlString.Create(ulTag.ToString()); }
public void PageLinks_WhenGivenPagingInformationAndHowToGenerateLinks_ShouldGenerateASeriesOfLinksForAllPages() { // Arrange // In order to test an extension method we need to create a null instance of the class the // extension method applies to HtmlHelper helper = null; PagingInfo pagingInfo = new PagingInfo { CurrentPage = 2, TotalItems = 28, ItemsPerPage = 10 }; Func<int, string> pageUrlDelegate = i => "Page" + i; // Act MvcHtmlString result = helper.PageLinks(pagingInfo, pageUrlDelegate); // Assert Assert.AreEqual(expected: @"<a href=""Page1"">1</a>" + @"<a class=""selected"" href=""Page2"">2</a>" + @"<a href=""Page3"">3</a>", actual: result.ToString()); }
public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int, string> pageUrl) { StringBuilder result = new StringBuilder(); for (int i = 1; i <= pagingInfo.TotalPages; i++) { // Build an anchor tag TagBuilder tag = new TagBuilder("a"); tag.MergeAttribute("href", pageUrl(i)); tag.InnerHtml = i.ToString(); if (i == pagingInfo.CurrentPage) tag.AddCssClass("selected"); // Append it to the string builder result.AppendLine(tag.ToString()); } // Feed the string builder to the MvcHtmlString static method // to return an MvcHtmlString return MvcHtmlString.Create(result.ToString()); }
public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int, string> pageUrl) { StringBuilder result = new StringBuilder(); TagBuilder ul = new TagBuilder("ul"); ul.AddCssClass("pagination"); for (int i = 1; i <= pagingInfo.TotalPages; i++) { TagBuilder li = new TagBuilder("li"); TagBuilder a = new TagBuilder("a"); a.MergeAttribute("href", pageUrl(i)); a.InnerHtml = i.ToString(); if (i == pagingInfo.CurrentPage) { li.AddCssClass("active"); } li.InnerHtml = a.ToString(); result.Append(li.ToString()); } ul.InnerHtml = result.ToString(); return MvcHtmlString.Create(ul.ToString()); }