Created
February 27, 2010 01:42
-
-
Save rbazinet/316392 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| public static class ListHelpers { | |
| public static void Repeater<T>(this HtmlHelper html | |
| , IEnumerable<T> items | |
| , Action<T> render | |
| , Action<T> renderAlt) { | |
| if (items == null) | |
| return; | |
| int i = 0; | |
| items.ToList().ForEach(item => { | |
| if (i++ % 2 == 0) | |
| render(item); | |
| else | |
| renderAlt(item); | |
| }); | |
| } | |
| public static void Repeater<T>(this HtmlHelper html | |
| , Action<T> render | |
| , Action<T> renderAlt) { | |
| var items = html.ViewContext.ViewData as IEnumerable<T>; | |
| html.Repeater(items, render, renderAlt); | |
| } | |
| public static void Repeater<T>(this HtmlHelper html | |
| , string viewDataKey | |
| , Action<T> render | |
| , Action<T> renderAlt) { | |
| var items = html.ViewContext.ViewData as IEnumerable<T>; | |
| var viewData = html.ViewContext.ViewData as IDictionary<string, object>; | |
| if (viewData != null) { | |
| items = viewData[viewDataKey] as IEnumerable<T>; | |
| } else { | |
| items = new ViewDataDictionary(viewData)[viewDataKey] as IEnumerable<T>; | |
| } | |
| html.Repeater(items, render, renderAlt); | |
| } | |
| public static void Repeater<T>(this HtmlHelper html | |
| , IEnumerable<T> items | |
| , string className | |
| , string classNameAlt | |
| , Action<T, string> render) | |
| { | |
| if (items == null) | |
| return; | |
| int i = 0; | |
| foreach (var item in items) { | |
| render(item, (i++ % 2 == 0) ? className : classNameAlt); | |
| } | |
| } | |
| } |
This file contains hidden or 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
| namespace System.Collections.Generic | |
| { | |
| public interface IPagedList | |
| { | |
| int TotalCount | |
| { | |
| get; | |
| set; | |
| } | |
| int CurrentPage { | |
| get; | |
| } | |
| int NextPage { | |
| get; | |
| } | |
| int LastPage { | |
| get; | |
| } | |
| int TotalPages { | |
| get; | |
| set; | |
| } | |
| int PageIndex | |
| { | |
| get; | |
| set; | |
| } | |
| int PageSize | |
| { | |
| get; | |
| set; | |
| } | |
| bool HasPreviousPage | |
| { | |
| get; | |
| } | |
| bool HasNextPage | |
| { | |
| get; | |
| } | |
| bool IsCurrentPage(int pageNumber); | |
| } | |
| public class PagedList<T> : List<T>, IPagedList | |
| { | |
| public PagedList(IQueryable<T> source, int index, int pageSize) | |
| { | |
| this.TotalCount = source.Count(); | |
| this.PageSize = pageSize; | |
| this.PageIndex = index; | |
| this.TotalPages = 1; | |
| CalcPages(); | |
| this.AddRange(source.Skip(index * pageSize).Take(pageSize).ToList()); | |
| } | |
| void CalcPages() { | |
| if (PageSize > 0 && TotalCount > PageSize) { | |
| TotalPages = TotalCount / PageSize; | |
| if (TotalCount % PageSize > 0) | |
| TotalPages++; | |
| } | |
| } | |
| public int CurrentPage { | |
| get { | |
| return PageIndex + 1; | |
| } | |
| } | |
| public int NextPage { | |
| get { | |
| return CurrentPage + 1; | |
| } | |
| } | |
| public int LastPage { | |
| get { | |
| return CurrentPage - 1; | |
| } | |
| } | |
| public int TotalCount | |
| { | |
| get; | |
| set; | |
| } | |
| public int TotalPages { | |
| get; | |
| set; | |
| } | |
| public int PageIndex | |
| { | |
| get; | |
| set; | |
| } | |
| public int PageSize | |
| { | |
| get; | |
| set; | |
| } | |
| public bool HasPreviousPage | |
| { | |
| get | |
| { | |
| return (PageIndex > 0); | |
| } | |
| } | |
| public bool HasNextPage | |
| { | |
| get | |
| { | |
| return (PageIndex * PageSize) <= TotalCount; | |
| } | |
| } | |
| public bool IsCurrentPage(int pageNumber) { | |
| return pageNumber==CurrentPage; | |
| } | |
| } | |
| public static class Pagination | |
| { | |
| public static PagedList<T> ToPagedList<T>(this IQueryable<T> source, int index, int pageSize) | |
| { | |
| return new PagedList<T>(source, index, pageSize); | |
| } | |
| public static PagedList<T> ToPagedList<T>(this IQueryable<T> source, int index) | |
| { | |
| return new PagedList<T>(source, index, 10); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment