Created
July 2, 2010 03:23
-
-
Save joshuaflanagan/460885 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 HtmlTag TableFor<T>(this IFubuPage view, IEnumerable<T> data, params string[] columns) where T : class | |
{ | |
var properties = typeof(T).GetProperties(); | |
// optionally limit the columns to display | |
if (columns.Length > 0) properties = properties.Where(p => columns.Contains(p.Name)).ToArray(); | |
var table = new HtmlTag("table"); | |
var header = new HtmlTag("thead"); | |
table.Child(header); | |
foreach (var property in properties) | |
{ | |
// you could new up a T() here and do LabelFor() instead | |
header.Add("th", th => th.Text(property.Name)); | |
} | |
var body = new HtmlTag("tbody"); | |
table.Child(body); | |
foreach (var item in data) | |
{ | |
// build a TagGenerator for each model instance | |
var tagGenerator = view.Tags(item); | |
var row = new HtmlTag("tr"); | |
body.Child(row); | |
foreach (var property in properties) | |
{ | |
// build an ElementRequest for each property | |
var request = tagGenerator.GetRequest(new SingleProperty(property)); | |
row.Add("td", td => td.Child(tagGenerator.DisplayFor(request))); | |
} | |
} | |
return table; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment