Created
April 5, 2013 16:47
-
-
Save jrusbatch/5320789 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 HtmlHelperExtensions | |
{ | |
public static WebGrid<T> Grid<T>( | |
this HtmlHelper<PagedViewModel<T>> htmlHelper, | |
int rowsPerPage = 10) where T : class | |
{ | |
return new WebGrid<T>(htmlHelper, rowsPerPage); | |
} | |
public static WebGrid<TModel> Grid<T, TModel>( | |
this HtmlHelper<T> htmlHelper, | |
int rowsPerPage = 10) where T : PagedViewModel<TModel> where TModel : class | |
{ | |
return new WebGrid<TModel>(htmlHelper, rowsPerPage); | |
} | |
} |
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 class WebGrid<T> : WebGrid where T : class | |
{ | |
private readonly HtmlHelper htmlHelper; | |
internal WebGrid(HtmlHelper helper, int rowsPerPage = 10) | |
: this(helper, null, rowsPerPage) { } | |
internal WebGrid(HtmlHelper helper, IEnumerable<T> source, int rowsPerPage = 10) | |
: base(source, rowsPerPage: rowsPerPage) | |
{ | |
htmlHelper = helper; | |
} | |
public WebGridColumn Column( | |
string header = null, | |
Func<T, object> format = null, | |
string style = null, | |
bool canSort = true) | |
{ | |
return Column(x => x, header, format, style, canSort); | |
} | |
public WebGridColumn Column<TProperty>( | |
Expression<Func<T, TProperty>> expression, | |
string header = null, | |
Func<T, object> format = null, | |
string style = null, | |
bool canSort = true) | |
{ | |
string column = null; | |
if (expression != null) | |
{ | |
var modelMetadata = ModelMetadata.FromLambdaExpression(expression, new ViewDataDictionary<T>()); | |
column = ExpressionHelper.GetExpressionText(expression); | |
if (header == null) | |
{ | |
header = modelMetadata.DisplayName; | |
} | |
if (format == null) | |
{ | |
var accessor = expression.Compile(); | |
if (!string.IsNullOrEmpty(modelMetadata.TemplateHint)) | |
{ | |
format = x => htmlHelper.Partial(modelMetadata.TemplateHint, accessor(x), new ViewDataDictionary()); | |
} | |
else if (!string.IsNullOrEmpty(modelMetadata.DisplayFormatString)) | |
{ | |
format = x => new HtmlString(HttpUtility.HtmlEncode(string.Format(modelMetadata.DisplayFormatString, accessor(x)))); | |
} | |
} | |
} | |
Func<dynamic, object> wrappedFormat = null; | |
if (format != null) | |
{ | |
wrappedFormat = o => format((T)o.Value); | |
} | |
return Column(column, header, wrappedFormat, style, canSort); | |
} | |
public WebGrid<T> Bind(IEnumerable<T> source, IEnumerable<string> columnNames = null, bool autoSortAndPage = true, int rowCount = -1) | |
{ | |
Bind((source ?? Enumerable.Empty<T>()).Cast<object>(), columnNames, autoSortAndPage, rowCount); | |
return this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment