public static HtmlString PagedList(this IHtmlHelper helper, int pageIndex, int totalPages, Func <int, string> page, QPageOptions options) { return(new HtmlString(Generate(pageIndex, totalPages, page, options))); }
private static List <TagBuilder> GetPageListItems(int pageIndex, int totalPages, Func <int, string> page, QPageOptions options) { var Items = new List <TagBuilder>(); int perPage = GetItemPerPage(); pageIndex = pageIndex == 0 ? 1 : pageIndex; int allPages = totalPages / perPage; int start = (pageIndex / perPage) * GetItemPerPage(); start = start == totalPages ? start - perPage : start; int end = start + perPage; if (end > allPages) { end = allPages; } if (HasPrevPage(pageIndex)) { Items.Add(GetPrevPage(options.PrevPageTitle, page(pageIndex - 1))); } if (end - start > GetItemPerPage()) { end = start + GetItemPerPage(); } if (start == 0) { start = 1; } if ((totalPages % perPage) > 0 && (allPages * perPage) < totalPages) { end++; } for (int i = start; i <= end; i++) { var li = GenerateItem(i.ToString(), page(i)); if (i == pageIndex) { li.AddCssClass("active"); } Items.Add(li); } if (HasNextPage(pageIndex, totalPages)) { Items.Add(GetNextPage(options.NextPageTitle, page(pageIndex + 1))); } return(Items); }
private static string Generate(int pageIndex, int totalPages, Func <int, string> page, QPageOptions options) { var Items = GetPageListItems(pageIndex, totalPages, page, options); var ul = new TagBuilder("ul"); ul.AddCssClass("pagination"); var ItemsString = Items.Aggregate( new StringBuilder(), (sb, listItem) => sb.Append(TagBuilderToString(listItem)), sb => sb.ToString()); ul.InnerHtml.AppendHtml(ItemsString); return(TagBuilderToString(ul)); }